首页 > 解决方案 > 在 python-pptx 中复制一个自由形式的对象

问题描述

我在模板幻灯片中有一个自由形式的对象,我想复制并制作多个对象。

我在文档中找不到从预先存在的形状创建形状的方法。我是在找错地方还是该功能不存在?

标签: pythonpowerpointpython-pptx

解决方案


API 不支持此python-pptx操作,但您可以使用一些内部函数来完成此结果

from copy import deepcopy

# ---get the existing freeform shape however you do---
freeform = slide.shapes[n]
# ---get the underlying XML element for that shape---
sp = freeform._sp
for idx in range(3):
    # ---duplicate original freeform---
    new_sp = deepcopy(sp)
    # ---create a unique id for it---
    new_sp.nvSpPr.cNvPr.id = 1000 + idx
    # ---insert it after original---
    sp.addnext(new_sp)

这些都将直接堆叠在原始顶部,因此您可能需要添加一些以将它们移动到新位置。此外,如果现有的自由格式参与超链接,您可能会遇到麻烦,无论是本身是链接还是它包含的具有超链接的文本。


推荐阅读