Python使用pptx实现复制页面到其他PPT中
更新时间:2023年02月02日 10:19:13 作者:虚坏叔叔
这篇文章主要为大家详细介绍了python如何使用pptx库实现从一个ppt复制页面到另一个ppt里面,文中的示例代码讲解详细,感兴趣的可以尝试一下
一、原理
如题,我有一个模板课件.pptx:

其内容:

我想复制模板中间的某一页多次,比如复制第1页,然后复制3次,
prs = Presentation(r"D:\自动化\课件.pptx")
for i in range(0,3):
copied_slide = duplicate_slide(prs, 0)
次数是根据我的需求指定的,使用python pptx模块复制,
def duplicate_slide(pres,index):
template = pres.slides[index]
blank_slide_layout = pres.slide_layouts[index]
copied_slide = pres.slides.add_slide(blank_slide_layout)
for shp in template.shapes:
el = shp.element
newel = copy.deepcopy(el)
copied_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
for _, value in six.iteritems(template.part.rels):
# Make sure we don't copy a notesSlide relation as that won't exist
if "notesSlide" not in value.reltype:
copied_slide.part.rels.add_relationship(value.reltype,
value._target,
value.rId)
return copied_slide
然后保存成另一个pptx文件
path = r'D:\自动化\result.pptx' prs.save(path)

复制后的ppt内容

二、所有代码
import copy,six
from pptx import Presentation
def duplicate_slide(pres,index):
template = pres.slides[index]
blank_slide_layout = pres.slide_layouts[index]
copied_slide = pres.slides.add_slide(blank_slide_layout)
for shp in template.shapes:
el = shp.element
newel = copy.deepcopy(el)
copied_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
for _, value in six.iteritems(template.part.rels):
# Make sure we don't copy a notesSlide relation as that won't exist
if "notesSlide" not in value.reltype:
copied_slide.part.rels.add_relationship(value.reltype,
value._target,
value.rId)
return copied_slide
prs = Presentation(r"D:\自动化\课件.pptx")
for i in range(0,3):
copied_slide = duplicate_slide(prs, 0)
path = r'D:\自动化\result.pptx'
prs.save(path)
到此这篇关于Python使用pptx实现复制页面到其他PPT中的文章就介绍到这了,更多相关Python pptx复制PPT页面内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
相关文章
Python把csv数据写入list和字典类型的变量脚本方法
今天小编就为大家分享一篇Python把csv数据写入list和字典类型的变量脚本方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-06-06
Python数据分析之双色球统计单个红和蓝球哪个比例高的方法
这篇文章主要介绍了Python数据分析之双色球统计单个红和蓝球哪个比例高的方法,涉及Python数值运算及图形绘制相关操作技巧,需要的朋友可以参考下2018-02-02
Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法
这篇文章主要介绍了Python实现将MySQL数据库表中的数据导出生成csv格式文件的方法,涉及Python针对mysql数据库的连接、查询、csv格式数据文件的生成等相关操作技巧,需要的朋友可以参考下2018-01-01
Python SqlAlchemy动态添加数据表字段实例解析
这篇文章主要介绍了Python SqlAlchemy动态添加数据表字段实例解析,分享了相关代码示例,小编觉得还是挺不错的,具有一定借鉴价值,需要的朋友可以参考下2018-02-02


最新评论