通過(guò)python-pptx模塊操作ppt文件的方法
ppt通過(guò)其精美的可視化技巧以及良好的演示效果,成為了職場(chǎng)人士的必備技能。ppt的設(shè)計(jì)是一門(mén)大學(xué)問(wèn),無(wú)論是設(shè)計(jì)技巧,還是操作方法,都衍生出了專門(mén)的課程。
本文主要介紹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
上述分類(lèi)對(duì)應(yīng)的常用操作如下
1. presentations
用于打開(kāi),創(chuàng)建,保存ppt文檔,用法如下
>>> from pptx import Presentation# 創(chuàng)建新的ppt文檔>>> prs = Presentation()# 打開(kāi)一個(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
通過(guò)數(shù)字下標(biāo)0到9來(lái)訪問(wèn),指定布局添加一頁(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表示所有基本元素的和, 通過(guò)如下方式來(lái)訪問(wèn)對(duì)應(yīng)的shapes
shapes = slide.shapes
對(duì)于shapes而言,我們可以獲取和設(shè)置其各種屬性,比如最常用的text屬性,用法如下
>>> shapes.text = ’hello world’
還可以通過(guò)add系列方法來(lái)添加各種元素,添加文本框的方法如下
>>> 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的子集, 通過(guò)數(shù)字下標(biāo)來(lái)訪問(wèn)對(duì)應(yīng)的placeholder,用法如下
>>> slide.placeholders[1]<pptx.shapes.placeholder.SlidePlaceholder object at 0x03F73A90>>>> slide.placeholders[1].placeholder_format.idx1>>> slide.placeholders[1].name’Subtitle 2’
placeholders是頁(yè)面上已有的元素,獲取對(duì)應(yīng)的placeholders之后,可以通過(guò)insert系列方法來(lái)向其中新添元素。
了解上述層級(jí)結(jié)構(gòu),有助于我們對(duì)ppt的讀寫(xiě)操作。除了寫(xiě)操作之外,也可以通過(guò)讀操作來(lái)批量提取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)
通過(guò)該模塊,可以快速搭建ppt的基本框架,也可以批量提取ppt中的特定元素,比如提取文字轉(zhuǎn)換成word, 或者提取表格轉(zhuǎn)換成excel文件。總而言之,該模塊適合替代大量繁瑣的人工復(fù)制粘貼操作。
到此這篇關(guān)于通過(guò)python-pptx模塊操作ppt文件的方法的文章就介紹到這了,更多相關(guān)python-pptx模塊操作ppt文件內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. CSS3實(shí)例分享之多重背景的實(shí)現(xiàn)(Multiple backgrounds)2. XML入門(mén)的常見(jiàn)問(wèn)題(三)3. WMLScript腳本程序設(shè)計(jì)第1/9頁(yè)4. Vue3獲取DOM節(jié)點(diǎn)的3種方式實(shí)例5. 前端html+css實(shí)現(xiàn)動(dòng)態(tài)生日快樂(lè)代碼6. 詳解CSS偽元素的妙用單標(biāo)簽之美7. 利用CSS3新特性創(chuàng)建透明邊框三角8. vue實(shí)現(xiàn)將自己網(wǎng)站(h5鏈接)分享到微信中形成小卡片的超詳細(xì)教程9. 不要在HTML中濫用div10. XML 取得元素的字符數(shù)據(jù)
