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

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

一、原理

如题,我有一个模板课件.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

标签:Python,pptx,复制,PPT,页面
0
投稿

猜你喜欢

  • AlternatingItemTemplate类似于 ItemTemplate 元素

    2023-07-14 13:09:07
  • python实现代理服务功能实例

    2023-10-04 05:36:57
  • 美图秀秀web开放平台--PHP流式上传和表单上传示例分享

    2023-11-15 08:34:42
  • Python常用模块介绍

    2021-01-03 10:11:53
  • python中的各种运算符介绍

    2021-08-09 12:49:04
  • 《写给大家看的设计书》阅读笔记之色彩

    2009-07-30 12:45:00
  • Python叠加两幅栅格图像的实现方法

    2023-07-25 17:14:06
  • Thinkphp5.0 框架的请求方式与响应方式分析

    2023-11-15 00:07:09
  • php session应用实例 登录验证

    2023-11-14 13:49:43
  • python编程中简洁优雅的推导式示例详解

    2021-02-12 01:43:56
  • Python面向对象之静态属性、类方法与静态方法分析

    2021-03-28 15:14:22
  • php5.2 Json不能正确处理中文、GB编码的解决方法

    2023-10-26 13:49:28
  • 使用 Python 实现简单的 switch/case 语句的方法

    2021-02-02 09:10:16
  • 纯CSS下拉菜单代码

    2008-09-10 12:35:00
  • 详解在Python的Django框架中创建模板库的方法

    2023-08-15 10:32:56
  • 在python代码中加入环境变量的语句操作

    2023-03-01 22:58:19
  • Django中ajax发送post请求 报403错误CSRF验证失败解决方案

    2021-06-11 19:47:32
  • 使用有趣的自定义标记布局页面

    2012-07-12 01:29:03
  • Dreamweaver4探谜系列(2)

    2010-05-13 12:08:00
  • Web设计的成就感

    2009-09-04 19:02:00
  • asp之家 网络编程 m.aspxhome.com