欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

使用python-pptx創(chuàng)建PPT演示文檔功能實(shí)踐

 更新時(shí)間:2023年06月21日 17:19:22   作者:肖永威  
這篇文章主要介紹了使用python-pptx創(chuàng)建PPT演示文檔功能實(shí)踐,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

python對(duì)PPT演示文檔讀寫,是通過(guò)第三方庫(kù)python-pptx實(shí)現(xiàn)的,python-pptx是用于創(chuàng)建和更新 PowerPoint(.pptx)文件的 Python 庫(kù)。

關(guān)于PPT演示文檔與幻燈片模板的內(nèi)容不是本文的重點(diǎn),在此略過(guò)。

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:幻燈片,就是演示文稿中每一頁(yè)的頁(yè)面。
  • Shape:形狀,在每頁(yè)幻燈片內(nèi)插入的方框,可以是形狀,也可以是文本框、圖片、表格等等。
  • Run:文字塊,一般為較少字符。
  • Paragraph:段落,通常有序號(hào)?、1.等。

圖示演示文檔,如下圖所示,由3頁(yè)幻燈片(slide)構(gòu)成,其中,第三頁(yè)幻燈片中的形狀(shape)分別是“標(biāo)題 1”(Title 1)和“圖片”(Picture Placeholder 2)組成。

1.2. 自定義幻燈片母版

使用程序生成演示文檔,最好先自定義幻燈片母版,如下圖所示,定義4頁(yè)模板(slide_layouts)。

注意:shape名稱在office軟件下,是中文,而程序讀出來(lái)的可能是英文!

對(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')
# 獲取某一頁(yè)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)建新的演示文檔

這里所說(shuō)模板仍然是pptx文件,不是PPT的模板(potx),python-pptx無(wú)法讀取potx模板。

也是說(shuō),使用僅有一頁(yè),并且無(wú)內(nèi)容的空演示文檔,內(nèi)含自定義幻燈片母版,如前面所述的母版樣例。

創(chuàng)建新演示文檔過(guò)程如下:

  • 首先,修改首頁(yè)內(nèi)容,例如主題和副主題
  • 接著,按實(shí)際要求,使用具體模板,也就是slide_layouts

代碼過(guò)程如下:

from pptx import Presentation
from PIL import Image
im=Image.open('d:\\02資料\\AI無(wú)感加油.png')
# 修改首頁(yè)
prs= Presentation('template_1.pptx')
slide = prs.slides[0]
title = ['油站全流程診斷輸出測(cè)試頁(yè)',
        '測(cè)試頁(yè)副標(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è)母版生成一頁(yè)ppt
for shape in slide.placeholders:         # 獲取這一頁(yè)所有的占位符
    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ù)庫(kù))讀取的圖片,臨時(shí)存儲(chǔ)
slide = prs.slides.add_slide(prs.slide_layouts[2])  # 用第一個(gè)母版生成一頁(yè)ppt
for shape in slide.placeholders:         # 獲取這一頁(yè)所有的占位符
    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

對(duì)于數(shù)據(jù)分析結(jié)果規(guī)范化輸出,python-pptx功能基本滿足,使用條件是精通PPT,設(shè)計(jì)出合適的母版,供程序交互使用。

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論