python-pptx模块
可以创建、修改PPT(.pptx)文件
需要单独安装,不包含在Python标准模块里
导入的时候直接使用: import pptx 即可,如果没有报错则代表安装成功
PPT结构
主要四部分:Slide、Shape、Paragraph和Run
获取slide
.slide 得到一个列表,包含每一个slide
import os
os.chdir("D:\\python_major\\auto_office16")
from pptx import Presentation
prs = Presentation("demo.pptx")
for slide in prs.slides:
print(slide)
–> 输出结果为:
获取形状Shape
slide.shape 获取形状
import os
os.chdir("D:\\python_major\\auto_office16")
from pptx import Presentation
prs = Presentation("demo.pptx")
for slide in prs.slides:
for shape in slide.shapes:
print(shape)
–> 输出结果为:
判断一个shape中是否存在文字
shape.has_text_frame 是否有文字
shape.text_frame 获取文字框
import os
os.chdir("D:\\python_major\\auto_office16")
from pptx import Presentation
prs = Presentation("demo.pptx")
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
text_frame = shape.text_frame
print(text_frame.text)
–> 输出结果为:
从shape中找paragraph
text_frame.paragraphs 获取shape中的段落
text_frame = shape.text_frame
for paragraph in text_frame.paragraphs:
print(paragraph.text)
综合应用
编写一个Python程序,要求
(1) 打开Bilibili 2Q19 Investor Presentation-Final.pptx
(2)按照paragraph分段,转换成为word文档
(3)保存为Bilibili 2Q19 Investor Presentation-Final.docx
参考代码
import os
os.chdir("D:\\python_major\\auto_office16")
from pptx import Presentation
from docx import Document
doc = Document()
prs = Presentation("Bilibili 2Q19 Investor Presentation-Final.pptx")
ls = []
for slide in prs.slides:
for shape in slide.shapes:
if shape.has_text_frame:
text_frame = shape.text_frame
for paragraph in text_frame.paragraphs:
if paragraph.text != '':
doc.add_paragraph(paragraph.text)
doc.save("Bilibili 2Q19 Investor Presentation-Final.docx")
–> 输出结果为: