在 python-pptx 中复制自由形式对象

Copy a freeform object in python-pptx

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

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

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

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)

这些都将直接堆叠在原件之上,因此您可能想要添加一些以将它们移动到新位置。此外,如果现有的自由形式参与 hyperlink,您可能 运行 遇到麻烦,无论是本身是 link 还是它包含的文本具有 hyperlink.