1.安装:
pip install python-pptx
pip install python-pptx -i https://pypi.tuna.tsinghua.edu.cn/simple
2.初步认识并创建ppt
from pptx import Presentation #导入Presentation prs = Presentation() #文件,新建一个文件 title_slide_layout = prs.slide_layouts[0] # 用文件中的第一个样式 slide = prs.slides.add_slide(title_slide_layout) #幻灯片使用文件的第一个样式,根据第一个样式建立幻灯片 title = slide.shapes.title #幻灯片中的标题 subtitle = slide.placeholders[1] #幻灯片中的副标题 title.text = "Hello, World!" #给幻灯片标题中的文本赋值 subtitle.text = "python-pptx 可以轻松制作powerpoint!" #给副标题文本赋值 prs.save('czl.pptx') #保存文件
3.遍历打印模板中样式中的文本框中段落的文字
from pptx import Presentation #导入 wenjian = Presentation("moban.pptx") #打开模板PPT #wenjian.slides,指所有内容 for mb in wenjian.slides: #遍历打印所有母版 print(mb) for yangshi in mb.shapes: #mb.shapes,指母版中的所有样式,遍历打印所有样式 print(yangshi) if yangshi.has_text_frame: #yangshi.has_text_frame,表示样式中有文字 wenbenkuang = yangshi.text_frame #获取样式中的文本框 for duanluo in wenbenkuang.paragraphs: #wenbenkuang.paragraphs,表示文本框中的段落 print(duanluo) #打印段落 # wenzi = wenbenkuang.text #获取文本框中的文字 wenzi = duanluo.text #段落中的文字 print(wenzi)
4.保存PPT中文字到word文档中
# encoding=utf8 #-*-coding:utf-8 -*- #pip install python-pptx -i https://pypi.tuna.tsinghua.edu.cn/simple #pip install python-docx -i https://pypi.tuna.tsinghua.edu.cn/simple from pptx import Presentation #导入PPT库 from docx import Document #导入word库 myword = Document() #定义一个word文档 wenjian = Presentation("moban.pptx") #打开模板PPT #wenjian.slides,指所有内容 for mb in wenjian.slides: #遍历打印所有母版 print(mb) for yangshi in mb.shapes: #mb.shapes,指母版中的所有样式,遍历打印所有样式 print(yangshi) if yangshi.has_text_frame: #yangshi.has_text_frame,表示样式中有文字 wenbenkuang = yangshi.text_frame #获取样式中的文本框 for duanluo in wenbenkuang.paragraphs: #wenbenkuang.paragraphs,表示文本框中的段落 print(duanluo) #打印段落 # wenzi = wenbenkuang.text #获取文本框中的文字 wenzi = duanluo.text #段落中的文字 print(wenzi) myword.add_paragraph(wenzi) #将文字添加到word文档中 myword.save("1.docx") #保存word文档为1.docx
5.获取PPT模板中的一张幻灯片中的占位符,然后根据这个模板新增加一个幻灯片,并向新添加的幻灯片中的占位符中赋值
# encoding=utf8 #-*-coding:utf-8 -*- #pip install python-pptx -i https://pypi.tuna.tsinghua.edu.cn/simple #pip install python-docx -i https://pypi.tuna.tsinghua.edu.cn/simple from pptx import Presentation #导入PPT库 from docx import Document #导入word库 myword = Document() #定义一个word文档 wenjian = Presentation("moban.pptx") #打开模板PPT diyigeyangshi = wenjian.slide_layouts[0] #获取模板PPT文件中的第一个样式 huandengpian = wenjian.slides.add_slide(diyigeyangshi) #添加一个幻灯片,使用模板PPT中第一个样式 for zhanweifu in huandengpian.placeholders: #遍历幻灯片中的所有占位符 xinxi = zhanweifu.placeholder_format #获取占位符中的信息,即内容 print("占位符中的信息:") print(xinxi) zhanweifu_id = xinxi.idx #占位符的索引 print("占位符的索引:") print(zhanweifu_id) zhanweifu_name = zhanweifu.name #占位符的名称 print("占位符的名称:") print(zhanweifu_name) zhanweifu_type = xinxi.type #占位符的类型 print("占位符的类型:") print(zhanweifu_type) #往占位符中的text赋值 zhanweifu.text = "%s-%s-%s" % (zhanweifu_id,zhanweifu_name,zhanweifu_type) biaoti = huandengpian.placeholders[0] #幻灯篇中第一个占位符要填写的是标题 fubiaoti = huandengpian.placeholders[0] #幻灯片中第二个占位符要填写的是副标题 biaoti.text = "疫情期间应该做到什么?" #给标题的占位符中的文字内容赋值"疫情期间应该做到什么?" fubiaoti.text = "减少非必要的外出" #给副标题占位符的文字填写 "减少非必要的外出" wenjian.save("2.pptx") #保存PPT
6.给幻灯片中添加段落及设置等级
# encoding=utf8 #-*-coding:utf-8 -*- #pip install python-pptx -i https://pypi.tuna.tsinghua.edu.cn/simple #pip install python-docx -i https://pypi.tuna.tsinghua.edu.cn/simple from pptx import Presentation #导入PPT库 from docx import Document #导入word库 myword = Document() #定义一个word文档 wenjian = Presentation() #打开模板PPT diergeyangshi = wenjian.slide_layouts[1] #获取模板PPT文件中的第二个样式 huandengpian = wenjian.slides.add_slide(diergeyangshi) #添加一个幻灯片,使用模板PPT中第一个样式 biaoti = huandengpian.shapes.title #获取幻灯片 内容中的标题占位符 zhengwen = huandengpian.shapes.placeholders[1] #获取幻灯片 模板中的第二个占位符,即是写正文的地方 biaoti.text = '疫情期间如何不给国家添乱?' #给幻灯片中的标题占位符中的文字内容处填写 '疫情期间如何不给国家添乱?' zhengwen_wenbenkuang = zhengwen.text_frame #定位到正文占位符的文本框 zhengwen_wenbenkuang.text = '减少非必要的外出' #给文本框中的文字填写 '减少非必要的外出' duanluo = zhengwen_wenbenkuang.add_paragraph() #文本框中添加一个段落 duanluo.text = "戴口罩,勤洗手" #给段落占位符中添加文字内容"戴口罩,勤洗手" duanluo.level = 1 #给段落加一个等级 1 duanluo2 = zhengwen_wenbenkuang.add_paragraph() #文本框中添加一个段落 duanluo2.text = "一起学习" #给段落占位符中添加文字内容"戴口罩,勤洗手" duanluo2.level = 2 # 给段落添加一个等级2 duanluo3 = zhengwen_wenbenkuang.add_paragraph() #文本框中添加一个段落 duanluo3.text = "办公自动化" #给段落占位符中添加文字内容"戴口罩,勤洗手" wenjian.save("2.pptx") #保存PPT
7.
8.
9.
10.