使用python-pptx操作PPT的示例詳解
python對PPT演示文檔讀寫,是通過第三方庫python-pptx實現(xiàn)的,python-pptx是用于創(chuàng)建和更新 PowerPoint(.pptx)文件的 Python 庫。
關于PPT演示文檔與幻燈片模板的內(nèi)容不是本文的重點,在此略過。
1. PPT基本結(jié)構(gòu)在python-pptx下定義
1.1. 演示文檔結(jié)構(gòu)定義
python-pptx對ppt結(jié)構(gòu)的描述如下圖所示,演示文檔由多個幻燈片(slide)構(gòu)成,每個幻燈片由眾多各種形狀(shape)組成。
- Slide:幻燈片,就是演示文稿中每一頁的頁面。
- Shape:形狀,在每頁幻燈片內(nèi)插入的方框,可以是形狀,也可以是文本框、圖片、表格等等。
- Run:文字塊,一般為較少字符。
- Paragraph:段落,通常有序號?、1.等。
圖示演示文檔,如下圖所示,由3頁幻燈片(slide)構(gòu)成,其中,第三頁幻燈片中的形狀(shape)分別是“標題 1”(Title 1)和“圖片”(Picture Placeholder 2)組成。
1.2. 自定義幻燈片母版
使用程序生成演示文檔,最好先自定義幻燈片母版,如下圖所示,定義4頁模板(slide_layouts)。
注意:shape名稱在office軟件下,是中文,而程序讀出來的可能是英文!
對于母版內(nèi)容:
Slides_layouts:版式,一個幻燈片母版由多個版式組成,索引從0開始。
slide_layouts[]傳入0表示獲取的是第一個版式,傳入1表示獲取的是第二個版式
Placeholder:占位符:存在PPT母版里面的幻燈片的某一部件:Placeholder
2. python-pptx操作PPT實踐
2.1. 安裝python-pptx
pip install -i https://pypi.tuna.tsinghua.edu.cn/simple python-pptx
2.2. 讀取PPT演示文檔
from pptx import Presentation # 打開演示文檔 prs = Presentation('加油站全流程診斷大數(shù)據(jù)系統(tǒng).pptx') for slide in prs.slides: print(slide) # 寫入新文件中 prs.save('test.pptx') # 獲取某一頁Slide中的內(nèi)容 for i, slide in enumerate(prs.slides): if i == 3: for shape in slide.shapes: if shape.has_text_frame: text_frame = shape.text_frame print(text_frame.text)
2.3. 基于模板創(chuàng)建新的演示文檔
這里所說模板仍然是pptx文件,不是PPT的模板(potx),python-pptx無法讀取potx模板。也是說,使用僅有一頁,并且無內(nèi)容的空演示文檔,內(nèi)含自定義幻燈片母版,如前面所述的母版樣例。
創(chuàng)建新演示文檔過程如下:
- 首先,修改首頁內(nèi)容,例如主題和副主題
- 接著,按實際要求,使用具體模板,也就是slide_layouts
代碼過程如下:
from pptx import Presentation from PIL import Image im=Image.open('d:\\02資料\\AI無感加油.png') # 修改首頁 prs= Presentation('template_1.pptx') slide = prs.slides[0] title = ['油站全流程診斷輸出測試頁', '測試頁副標題'] for i, shape in enumerate(slide.shapes): if shape.has_text_frame: text_frame = shape.text_frame text_frame.text = title[i] # 插入內(nèi)容測試 slide = prs.slides.add_slide(prs.slide_layouts[1]) # 用第一個母版生成一頁ppt for shape in slide.placeholders: # 獲取這一頁所有的占位符 phf = shape.placeholder_format print(f'{phf.idx}--{shape.name}--{phf.type}') print('shape name ', shape.name) if shape.name == 'Title 1': shape.text = f'目標' #在標題占位符中填寫“目標” else: shape.text = f'內(nèi)容' #在其他占位符中填寫“內(nèi)容” # 插入圖片測試 im.save('tmp.png') #從外部(數(shù)據(jù)庫)讀取的圖片,臨時存儲 slide = prs.slides.add_slide(prs.slide_layouts[2]) # 用第一個母版生成一頁ppt for shape in slide.placeholders: # 獲取這一頁所有的占位符 phf = shape.placeholder_format print(f'{phf.idx}--{shape.name}--{phf.type}') print('shape name ', shape.name) if shape.name == 'Title 1': shape.text = f'插入圖片測試' #在標題占位符中填寫“目標” else: shape.insert_picture('tmp.png') #在其他占位符中填寫“圖片” prs.save('向占位符內(nèi)填寫內(nèi)容_1.pptx')
附加內(nèi)容:列出對象屬性。
for i, shape in enumerate(slide.shapes): print('對象類型', shape.shape_type) print('對象屬性列表', dir(shape))
shape部分屬性列表:
- ‘has_chart’,
- ‘has_table’,
- ‘has_text_frame’,
- ‘height’,
- ‘is_placeholder’,
- ‘name’,
- ‘part’,
- ‘placeholder_format’,
- ‘shape_id’,
- ‘shape_type’,
- ‘text’,
- ‘text_frame’,
- ‘top’,
- ‘width’
3. 小結(jié)
對于數(shù)據(jù)分析結(jié)果規(guī)范化輸出,python-pptx功能基本滿足,使用條件是精通PPT,設計出合適的母版,供程序交互使用。
以上就是使用python-pptx操作PPT的示例詳解的詳細內(nèi)容,更多關于python-pptx操作PPT的資料請關注腳本之家其它相關文章!
相關文章
地圖可視化神器kepler.gl python接口的使用方法
這篇文章主要介紹了python 地圖可視化神器kepler.gl近期重要更新的的相關資料,幫助大家利用python實現(xiàn)地圖可視化,感興趣的朋友可以了解下2020-12-12python的reverse函數(shù)翻轉(zhuǎn)結(jié)果為None的問題
這篇文章主要介紹了python的reverse函數(shù)翻轉(zhuǎn)結(jié)果為None的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05