【发布时间】:2020-03-25 13:13:16
【问题描述】:
我正在尝试合并 powerpoint 幻灯片,但我不知道该怎么做。我发现了一个看起来很有希望的 python 模块 pptx。我尝试将幻灯片内容从 2 个 powerpoint 复制到一个新的 powerpoint。但是我遇到了很多问题,比如如何获取现有的幻灯片布局或形状(包括图片、自动形状等所有形状)的高度、宽度、位置。我查看了Python-pptx: copy slide 上的 python-pptx 示例。我尝试做类似的事情,但没有奏效。
这是我的代码:
from pptx import Presentation
prs1 = Presentation("C:/Users/number/Documents/Test1.pptx")
prs2 = Presentation("C:/Users/number/Documents/Test2.pptx")
slidelst = []
for layout in prs2.slide_layouts:
slidelst.append(prs1.slides.add_slide(layout))
index = 0
for slide in slidelst:
for shape in prs2.slides[prs2.slides.index(slide)].shapes:
slide.shapes._spTree.insert_element_before(shape.element, 'p:extLst')
index+=1
prs1.save("C:/Users/I505168/Documents/newpresentation.pptx")
我得到错误:
Traceback (most recent call last):
File "C:\Users\I505168\Desktop\testpptx.py", line 12, in <module>
for shape in prs2.slides[prs2.slides.index(slide)].shapes:
File "C:\Users\I505168\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pptx\slide.py", line 315, in index
raise ValueError("%s is not in slide collection" % slide)
ValueError: <pptx.slide.Slide object at 0x03B53A50> is not in slide collection
预期结果是两张单独的幻灯片合并为一张幻灯片。
【问题讨论】:
-
嗨!我很想知道您是否最终找到了一种编程方式来做到这一点。
-
目前不支持从其他演示文稿复制幻灯片。 python-pptx.readthedocs.io/en/latest/user/…
-
@kingofjong 你解决问题了吗。如果是的话,你能分享一下解决方案吗
标签: python python-pptx