【发布时间】:2021-03-06 11:46:15
【问题描述】:
我的最终目标是更改演示文稿的主题。为此,我创建了一个源模板和新模板(具有正确的主题)。我遍历源模板中的每张幻灯片,然后使用下面的代码 - source 向新模板中添加一张新幻灯片,其中包含源的内容。如果有更好的方法来做到这一点,我很乐意听到。
这适用于文本和文本框,但是测试图像无法显示在新的 powerpoint 中(如下图所示):
代码
def copy_slide_from_external_prs(self, src, idx, newPrs):
# specify the slide you want to copy the contents from
src_slide = src.slides[idx]
# Define the layout you want to use from your generated pptx
slide_layout = newPrs.slide_layouts[2]
# create now slide, to copy contents to
curr_slide = newPrs.slides.add_slide(slide_layout)
# remove placeholders
for p in [s.element for s in curr_slide.shapes if 'Text Placeholder' in s.name or 'Title' in s.name]:
p.getparent().remove(p)
# now copy contents from external slide, but do not copy slide properties
# e.g. slide layouts, etc., because these would produce errors, as diplicate
# entries might be generated
for shp in src_slide.shapes:
el = shp.element
newel = copy.deepcopy(el)
curr_slide.shapes._spTree.insert_element_before(newel, 'p:extLst')
return newPrs
我尝试了许多不同的解决方案,并尝试使用源图像中的image.blob 属性创建新图片。但是,图像没有元素。我是否需要将 blob 转换为 PNG,保存它,然后使用保存的 PNG 创建新图像?
必须有更好的方法来做到这一点。再说一次,我只是想换个主题。
提前致谢!
【问题讨论】:
标签: image python-pptx