利用Python自動生成PPT的示例詳解
在日常工作中,PPT制作是常見的工作,如果制作創(chuàng)意類PPT,則無法通過自動化的形式生成,因為創(chuàng)意本身具有隨機性,而自動化解決的是重復性工作,兩者有所沖突。
python-pptx是python處理PPT的一個庫,注重的是讀和寫,無法導出,沒有渲染功能。

廢話不多說,第一步,安裝python-pptx庫:
pip3 install -i https://pypi.doubanio.com/simple/ python-pptx
ppt里面處理的主要對象一般為文本框,表格,圖片。
每一頁的ppt為一個slide
from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
#實例化一個ppt對象
ppt = Presentation("./test.pptx")
slide = ppt.slides[0] #第幾頁然后遍歷查看這一頁ppt中都包含哪些對象:
def rander_template(slide):
for shape in slide.shapes:
if shape.has_text_frame == True:
print("==========================文本框=============================")
print("段落長度:",len(shape.text_frame.paragraphs))
for paragraph in shape.text_frame.paragraphs:
# 拼接文字
print("段落包含字段:",len(paragraph.runs))
print(''.join(run.text for run in paragraph.runs))
for i in range(len(paragraph.runs)):
print("run"+str(i)+":"+paragraph.runs[i].text)
print(shape.text_frame.paragraphs[0].runs[0].text)
shape.text_frame.paragraphs[0].runs[0].text = "規(guī)則是自由的第一要義"
elif shape.has_table == True:
print("==========================表格==============================")
one_table_data = []
for row in shape.table.rows: # 讀每行
row_data = []
for cell in row.cells: # 讀一行中的所有單元格
cell.text = cell.text if cell.text != "" else "未填寫"
c = cell.text
row_data.append(c)
one_table_data.append(row_data) # 把每一行存入表
# 用二維列表輸出表格行和列的數(shù)據(jù)
print(one_table_data)
print("第一個單元格內(nèi)容:",shape.table.rows[0].cells[0].text)
elif isinstance(shape,Picture):
print("==========================圖片==============================")
index = 0
with open(f'{index}.jpg','wb') as f:
f.write(shape.image.blob)
index += 1
文本框?qū)ο蟆総ext_frame】:
shape.has_text_frame查看是否有文本框?qū)ο?,有的話查看具體有幾個段落【len(shape.text_frame.paragraphs)】,每個段落又有多少個run對象【len(paragraph.runs)】
注意:修改run對象的時候,修改run[0],后面的值都會被覆蓋。
表格對象【table】:
table對象還是按照行列值來定位劃分的,eg:table.rows[2]cells[3].text代表第三行第四列的值
圖片對象【Picture】:
插入圖片需要固定圖片的位置,比如:

def insert_pic(slide): #需要用到pptx庫的util方法 img_path = './blue.png' # 圖片路徑 # 設(shè)置圖片的位置和大小 left = util.Cm(8.04) top = util.Cm(9.93) width = util.Cm(15.07) height = util.Cm(4.06) # 在頁面中插入圖片 slide.shapes.add_picture(img_path, left, top, width, height)
全部代碼:
from pptx import Presentation, util
from pptx.util import Pt,Cm
from pptx.shapes.picture import Picture
ppt = Presentation("./test.pptx")
def rander_template(slide):
for shape in slide.shapes:
if shape.has_text_frame == True:
print("==========================文本框=============================")
print("段落長度:",len(shape.text_frame.paragraphs))
for paragraph in shape.text_frame.paragraphs:
# 拼接文字
print("段落包含字段:",len(paragraph.runs))
print(''.join(run.text for run in paragraph.runs))
for i in range(len(paragraph.runs)):
print("run"+str(i)+":"+paragraph.runs[i].text)
print(shape.text_frame.paragraphs[0].runs[0].text)
shape.text_frame.paragraphs[0].runs[0].text = "規(guī)則是自由的第一要義"
elif shape.has_table == True:
print("==========================表格==============================")
one_table_data = []
for row in shape.table.rows: # 讀每行
row_data = []
for cell in row.cells: # 讀一行中的所有單元格
cell.text = cell.text if cell.text != "" else "未填寫"
c = cell.text
row_data.append(c)
one_table_data.append(row_data) # 把每一行存入表
# 用二維列表輸出表格行和列的數(shù)據(jù)
print(one_table_data)
print("第一個單元格內(nèi)容:",shape.table.rows[0].cells[0].text)
elif isinstance(shape,Picture):
print("==========================圖片==============================")
index = 0
with open(f'{index}.jpg','wb') as f:
f.write(shape.image.blob)
index += 1
def insert_pic(slide):
img_path = './blue.png' # 圖片路徑
# 設(shè)置圖片的位置和大小
left = util.Cm(8.04)
top = util.Cm(9.93)
width = util.Cm(15.07)
height = util.Cm(4.06)
# 在頁面中插入圖片
slide.shapes.add_picture(img_path, left, top, width, height)
if __name__ == "__main__":
slide = ppt.slides[0] #第幾頁
rander_template(slide)
insert_pic(slide)
ppt.save('new.pptx') # 保存為文件初始ppt:

生成ppt:

到此這篇關(guān)于利用Python自動生成PPT的示例詳解的文章就介紹到這了,更多相關(guān)Python自動生成PPT內(nèi)容請搜索本站以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持本站!
版權(quán)聲明:本站文章來源標注為YINGSOO的內(nèi)容版權(quán)均為本站所有,歡迎引用、轉(zhuǎn)載,請保持原文完整并注明來源及原文鏈接。禁止復制或仿造本網(wǎng)站,禁止在非maisonbaluchon.cn所屬的服務器上建立鏡像,否則將依法追究法律責任。本站部分內(nèi)容來源于網(wǎng)友推薦、互聯(lián)網(wǎng)收集整理而來,僅供學習參考,不代表本站立場,如有內(nèi)容涉嫌侵權(quán),請聯(lián)系alex-e#qq.com處理。
關(guān)注官方微信