现有演示文稿中每张 pptx 幻灯片的形状 numbers/indexes
Shape numbers/indexes of each pptx slide within existing presentation
我是 python pptx 库的新手,我的问题是:如何定义形状列表、形状 numbers/indexes (shapetree) 和现有 pptx 幻灯片中每个 pptx 幻灯片的形状类型使用 Python 库 pptx 进行演示?我想更新现有的 ppt 演示文稿,似乎第一步是在每张幻灯片上找到准确的形状标识符,以便通过更新访问它们。你能指出一个现有的解决方案或可能的例子吗?
我假设“定义”是指“发现”之类的意思,因为通常没有充分的理由更改现有值。
开始的一个好方法是遍历并打印一些属性:
prs = Presentation("my-deck.pptx")
for slide in prs.slides:
for shape in slide.shapes:
print("id: %s, type: %s" % (shape.shape_id, shape.shape_type))
您可以根据需要使用此处 API 文档中列出的任何幻灯片 and/or 形状属性进行详细说明:
https://python-pptx.readthedocs.io/en/latest/api/shapes.html#shape-objects-in-general
要通过 ID(或名称)查找形状,您需要这样的代码:
def find_shape_by_id(shapes, shape_id):
"""Return shape by shape_id."""
for shape in shapes:
if shape.shape_id == shape_id:
return shape
return None
或者如果你经常这样做,你可以使用 dict
来完成这项工作:
shapes_by_id = dict((s.shape_id, s) for s in shapes)
然后为您提供所有方便的方法,例如:
>>> 7 in shapes_by_id
True
>>> shapes_by_id[7]
<pptx.shapes.Shape object at 0x...>
我是 python pptx 库的新手,我的问题是:如何定义形状列表、形状 numbers/indexes (shapetree) 和现有 pptx 幻灯片中每个 pptx 幻灯片的形状类型使用 Python 库 pptx 进行演示?我想更新现有的 ppt 演示文稿,似乎第一步是在每张幻灯片上找到准确的形状标识符,以便通过更新访问它们。你能指出一个现有的解决方案或可能的例子吗?
我假设“定义”是指“发现”之类的意思,因为通常没有充分的理由更改现有值。
开始的一个好方法是遍历并打印一些属性:
prs = Presentation("my-deck.pptx")
for slide in prs.slides:
for shape in slide.shapes:
print("id: %s, type: %s" % (shape.shape_id, shape.shape_type))
您可以根据需要使用此处 API 文档中列出的任何幻灯片 and/or 形状属性进行详细说明:
https://python-pptx.readthedocs.io/en/latest/api/shapes.html#shape-objects-in-general
要通过 ID(或名称)查找形状,您需要这样的代码:
def find_shape_by_id(shapes, shape_id):
"""Return shape by shape_id."""
for shape in shapes:
if shape.shape_id == shape_id:
return shape
return None
或者如果你经常这样做,你可以使用 dict
来完成这项工作:
shapes_by_id = dict((s.shape_id, s) for s in shapes)
然后为您提供所有方便的方法,例如:
>>> 7 in shapes_by_id
True
>>> shapes_by_id[7]
<pptx.shapes.Shape object at 0x...>