亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

通過python-pptx模塊操作ppt文件的方法

 更新時(shí)間:2020年12月26日 12:38:43   作者:生信修煉手冊(cè)  
這篇文章主要介紹了通過python-pptx模塊操作ppt文件的方法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,本文給大家介紹的需要的朋友可以參考下

ppt通過其精美的可視化技巧以及良好的演示效果,成為了職場(chǎng)人士的必備技能。ppt的設(shè)計(jì)是一門大學(xué)問,無論是設(shè)計(jì)技巧,還是操作方法,都衍生出了專門的課程。

本文主要介紹python操作ppt的技巧,編程的優(yōu)勢(shì)在于處理速度,對(duì)于高大上的ppt設(shè)計(jì),還是需要"以人為本", 所以該模塊的使用場(chǎng)景主要是ppt基本元素的提取和添加,適合大量?jī)?nèi)容的轉(zhuǎn)化,比如word轉(zhuǎn)ppt, 減少大量繁瑣的人工操作,盡管提供了一些基本的樣式設(shè)計(jì),但是并不能滿足日常辦公對(duì)ppt美觀性的要求。

在該模塊中,將ppt拆分為了以下多個(gè)元素

1. presentations, 表示整個(gè)ppt文檔

2. sliders. 表示ppt文檔的每一頁(yè)

3. shapes

4. placeholders

上述分類對(duì)應(yīng)的常用操作如下

1. presentations

用于打開,創(chuàng)建,保存ppt文檔,用法如下

>>> from pptx import Presentation
# 創(chuàng)建新的ppt文檔
>>> prs = Presentation()
# 打開一個(gè)ppt文檔
>>> prs = Presentation('input.pptx')
# 保存ppt文檔
>>> prs.save('test.pptx')

2. slides

在創(chuàng)建一頁(yè)ppt時(shí),需要指定對(duì)應(yīng)的布局,在該模塊中, 內(nèi)置了以下9種布局

1. Title

2. Title and Content

3. Section Header

4. Two Content

5. Comparison

6. Title Only

7. Blank

8. Content with Caption

9. Picture with Caption

通過數(shù)字下標(biāo)0到9來訪問,指定布局添加一頁(yè)ppt的用法如下

>>> title_slide_layout = prs.slide_layouts[0]
>>> slide = prs.slides.add_slide(title_slide_layout)

3. shapes

shapes表示容器,在制作ppt時(shí),各種基本元素,比如文本框,表格,圖片等都占據(jù)了ppt的一個(gè)部分,或者矩形區(qū)域,或者其他各種自定義的形狀。shapes表示所有基本元素的和, 通過如下方式來訪問對(duì)應(yīng)的shapes

shapes = slide.shapes

對(duì)于shapes而言,我們可以獲取和設(shè)置其各種屬性,比如最常用的text屬性,用法如下

>>> shapes.text = 'hello world'

還可以通過add系列方法來添加各種元素,添加文本框的方法如下

>>> from pptx.util import Inches, Pt
>>> left = top = width = height = Inches(1)
>>> txBox = slide.shapes.add_textbox(left, top, width, height)
>>> tf = txBox.text_frame
>>> tf.text = "first paragraph"
>>> p = tf.add_paragraph()
>>> p.text = "second paragraph"

添加表格的方法如下

>>> rows = cols = 2
>>> left = top = Inches(2.0)
>>> width = Inches(6.0)
>>> height = Inches(0.8)
>>> table = shapes.add_table(rows, cols, left, top, width, height).table
>>> table.columns[0].width = Inches(2.0)
>>> table.columns[1].width = Inches(4.0)
>>> # write column headings
>>> table.cell(0, 0).text = 'Foo'
>>> table.cell(0, 1).text = 'Bar'

4. placeholders

shapes表示所有基本元素的總和,而placeholders則表示每一個(gè)具體的元素,所以placeholders是shapes的子集, 通過數(shù)字下標(biāo)來訪問對(duì)應(yīng)的placeholder,用法如下

>>> slide.placeholders[1]
<pptx.shapes.placeholder.SlidePlaceholder object at 0x03F73A90>
>>> slide.placeholders[1].placeholder_format.idx
1
>>> slide.placeholders[1].name
'Subtitle 2'

placeholders是頁(yè)面上已有的元素,獲取對(duì)應(yīng)的placeholders之后,可以通過insert系列方法來向其中新添元素。

了解上述層級(jí)結(jié)構(gòu),有助于我們對(duì)ppt的讀寫操作。除了寫操作之外,也可以通過讀操作來批量提取ppt中的特定元素,以文字為例,提取方式如下

from pptx import Presentation
 
prs = Presentation(path_to_presentation)
 
text_runs = []
 
for slide in prs.slides:
 for shape in slide.shapes:
  if not shape.has_text_frame:
   continue
  for paragraph in shape.text_frame.paragraphs:
   for run in paragraph.runs:
    text_runs.append(run.text)

通過該模塊,可以快速搭建ppt的基本框架,也可以批量提取ppt中的特定元素,比如提取文字轉(zhuǎn)換成word, 或者提取表格轉(zhuǎn)換成excel文件??偠灾?,該模塊適合替代大量繁瑣的人工復(fù)制粘貼操作。

到此這篇關(guān)于通過python-pptx模塊操作ppt文件的方法的文章就介紹到這了,更多相關(guān)python-pptx模塊操作ppt文件內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論