scanny / python-pptx

Create Open XML PowerPoint documents in Python
MIT License
2.37k stars 513 forks source link

Select a specific placeholder #769

Open sonomirco opened 2 years ago

sonomirco commented 2 years ago

Hi there, Is there a way where I can retrieve the purple text into the placeholders, so I can filter and select the placeholder I want to use adding an image or a text? I looked at the XML but that value is not stored, I don't know where I can find it. Any suggestions?

For a bit more of a contest, I want to generate reports passing a ppt tamplate file and a list of pictures and descriptions. The tool will look at the tamplate and repeat the single slide template, many times as many required to locate all the images, the problem is to put the description underneath the correct image, and this is why my question above 😬

image

scanny commented 2 years ago

That value is surely stored somewhere, otherwise it could not appear. Where did you look for it? I expect it is in the slide layout rather than the slide itself.

Delengowski commented 2 years ago

So the suggested work flow as described by Scanny, The MathWorks, and even Microsoft itself is to create templates using the slide master, which seems really great since you can name not only a SlideMaster but a SlideLayout and Placeholders on a SlideLayout but then comes the problem. You apply that SlideLayout from the SlideMaster to a new slide in your presentation. Those names from the Placeholders do no transition over and you're stuck trying to deduce the mapping yourself.

Scanny is correct in that the name you assign to the Placeholder is found on the Placeholder object on the SlideLayout found in the corresponding SlideMaster.

Its quite the mess, and even if you did this using COM through win32COM, VBA, or whatever other bindings you're in the same situation.

brokemeisteratwork commented 2 years ago

There is a simple trick. When I create slide, I copy all placeholder names to the shape names.

...
slide_new = presentation.slides.add_slide(layout)
for shape in slide_new.shapes:
            if not shape.is_placeholder:
                continue

            # Setting Placeholder name to shape of layout
            shape.name = layout.placeholders.get(shape.placeholder_format.idx).name

Of course you can do it with existing slides too.