网络编程
位置:首页>> 网络编程>> Python编程>> Python使用pptx实现复制页面到其他PPT中

Python使用pptx实现复制页面到其他PPT中

作者:虚坏叔叔  发布时间:2021-07-13 01:28:16 

标签:Python,pptx,复制,PPT,页面

一、原理

如题,我有一个模板课件.pptx:

Python使用pptx实现复制页面到其他PPT中

其内容:

Python使用pptx实现复制页面到其他PPT中

我想复制模板中间的某一页多次,比如复制第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)

Python使用pptx实现复制页面到其他PPT中

复制后的ppt内容

Python使用pptx实现复制页面到其他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)

来源:https://blog.csdn.net/biggbang/article/details/128839633

0
投稿

猜你喜欢

手机版 网络编程 asp之家 www.aspxhome.com