使用python-pptx操作PPT的示例詳解
python對(duì)PPT演示文檔讀寫,是通過第三方庫python-pptx實(shí)現(xiàn)的,python-pptx是用于創(chuàng)建和更新 PowerPoint(.pptx)文件的 Python 庫。
關(guān)于PPT演示文檔與幻燈片模板的內(nèi)容不是本文的重點(diǎn),在此略過。
1. PPT基本結(jié)構(gòu)在python-pptx下定義
1.1. 演示文檔結(jié)構(gòu)定義
python-pptx對(duì)ppt結(jié)構(gòu)的描述如下圖所示,演示文檔由多個(gè)幻燈片(slide)構(gòu)成,每個(gè)幻燈片由眾多各種形狀(shape)組成。
- Slide:幻燈片,就是演示文稿中每一頁的頁面。
- Shape:形狀,在每頁幻燈片內(nèi)插入的方框,可以是形狀,也可以是文本框、圖片、表格等等。
- Run:文字塊,一般為較少字符。
- Paragraph:段落,通常有序號(hào)?、1.等。
圖示演示文檔,如下圖所示,由3頁幻燈片(slide)構(gòu)成,其中,第三頁幻燈片中的形狀(shape)分別是“標(biāo)題 1”(Title 1)和“圖片”(Picture Placeholder 2)組成。
1.2. 自定義幻燈片母版
使用程序生成演示文檔,最好先自定義幻燈片母版,如下圖所示,定義4頁模板(slide_layouts)。
注意:shape名稱在office軟件下,是中文,而程序讀出來的可能是英文!
對(duì)于母版內(nèi)容:
Slides_layouts:版式,一個(gè)幻燈片母版由多個(gè)版式組成,索引從0開始。
slide_layouts[]傳入0表示獲取的是第一個(gè)版式,傳入1表示獲取的是第二個(gè)版式
Placeholder:占位符:存在PPT母版里面的幻燈片的某一部件:Placeholder
2. python-pptx操作PPT實(shí)踐
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)容,例如主題和副主題
- 接著,按實(shí)際要求,使用具體模板,也就是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 = ['油站全流程診斷輸出測(cè)試頁', '測(cè)試頁副標(biāo)題'] for i, shape in enumerate(slide.shapes): if shape.has_text_frame: text_frame = shape.text_frame text_frame.text = title[i] # 插入內(nèi)容測(cè)試 slide = prs.slides.add_slide(prs.slide_layouts[1]) # 用第一個(gè)母版生成一頁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'目標(biāo)' #在標(biāo)題占位符中填寫“目標(biāo)” else: shape.text = f'內(nèi)容' #在其他占位符中填寫“內(nèi)容” # 插入圖片測(cè)試 im.save('tmp.png') #從外部(數(shù)據(jù)庫)讀取的圖片,臨時(shí)存儲(chǔ) slide = prs.slides.add_slide(prs.slide_layouts[2]) # 用第一個(gè)母版生成一頁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'插入圖片測(cè)試' #在標(biāo)題占位符中填寫“目標(biāo)” else: shape.insert_picture('tmp.png') #在其他占位符中填寫“圖片” prs.save('向占位符內(nèi)填寫內(nèi)容_1.pptx')
附加內(nèi)容:列出對(duì)象屬性。
for i, shape in enumerate(slide.shapes): print('對(duì)象類型', shape.shape_type) print('對(duì)象屬性列表', 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é)
對(duì)于數(shù)據(jù)分析結(jié)果規(guī)范化輸出,python-pptx功能基本滿足,使用條件是精通PPT,設(shè)計(jì)出合適的母版,供程序交互使用。
以上就是使用python-pptx操作PPT的示例詳解的詳細(xì)內(nèi)容,更多關(guān)于python-pptx操作PPT的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Python實(shí)現(xiàn)自動(dòng)化網(wǎng)頁操作步驟
這篇文章主要介紹Python如何實(shí)現(xiàn)自動(dòng)化網(wǎng)頁操作,文中有詳細(xì)的流程步驟和代碼示例,對(duì)我們的學(xué)習(xí)或工作有一定的幫助,需要的朋友可以參考下2023-06-06python中字符串類型json操作的注意事項(xiàng)
這篇文章主要給大家介紹了python中字符串類型json操作的一些注意事項(xiàng),文中介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-05-05地圖可視化神器kepler.gl python接口的使用方法
這篇文章主要介紹了python 地圖可視化神器kepler.gl近期重要更新的的相關(guān)資料,幫助大家利用python實(shí)現(xiàn)地圖可視化,感興趣的朋友可以了解下2020-12-12python的reverse函數(shù)翻轉(zhuǎn)結(jié)果為None的問題
這篇文章主要介紹了python的reverse函數(shù)翻轉(zhuǎn)結(jié)果為None的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-05-05PyTorch的深度學(xué)習(xí)入門之PyTorch安裝和配置
這篇文章主要介紹了PyTorch的深度學(xué)習(xí)入門之PyTorch安裝和配置,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-06-06