1
votes

I am new to python pptx library and my question is: How can I define the list of shapes, the shape numbers/indexes (shapetree) and shape types of each pptx slide within an existing presentation using Python Library pptx? I would like to update an existing ppt presentation and it seems that the first step would be to locate exact shape identifiers on each slide to access them with the updates. Would you point me to an existing solution or possibly examples?

1

1 Answers

5
votes

I assume by "define" you mean something like "discover", since there's not usually a good reason to change the existing values.

A good way to start is by looping through and printing some attributes:

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))

You can get as elaborate as you want with this, using any of the slide and/or shape attributes listed in the API documentation here:
https://python-pptx.readthedocs.io/en/latest/api/shapes.html#shape-objects-in-general

To look up a shape by id (or name) you need code like this:

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

or if you doing a lot of it you can use a dict for that job:

shapes_by_id = dict((s.shape_id, s) for s in shapes)

Which then gives you all the handy methods like:

>>> 7 in shapes_by_id
True
>>> shapes_by_id[7]
<pptx.shapes.Shape object at 0x...>