python-pptx icon indicating copy to clipboard operation
python-pptx copied to clipboard

Dynamically replicating an existing slide and editing it

Open avranil26 opened this issue 5 years ago • 7 comments

I have a slide in my Template which I want to dynamically replicate and add at a certain position based on my data. I have attached attached a screenshot of the slide which I want to dynamically replicate based of no of customer segments and add then add their data. Presently I am saving each slide into different pptx files and then manually collating then into a final report Screenshot (51)

avranil26 avatar May 26 '20 04:05 avranil26

You cannot duplicate existing slides or copy slides from other presentations into your presentation with python-pptx. You will have to use vba for something like that.

mszbot avatar May 27 '20 12:05 mszbot

Is there any possibility to create a new presentation with the slide I want to duplicate 'n' number of times.

avranil26 avatar May 28 '20 06:05 avranil26

If you make the slide a layout in your slide Master it will be available as one of the enumerated layouts when you create a new slide. If you're not going to use the layouts that are on the slide master you can delete them then your layout will be the only one available.

CaptKludge avatar Jun 01 '20 18:06 CaptKludge

@CaptKludge note that when you do that, any shapes on the slide layout become "background" shapes on a slide created from that layout and cannot be accessed or of course changed either. This makes sense because whatever shapes are on a layout are "shared" or "common" between all the slides that use that layout.

@avranil26 in general, python-pptx is not set up for what you're talking about. The common approach would be to place whatever is absolutely static on the slide layout, and then write code to generate each of the remaining bits from scratch each time.

scanny avatar Jun 01 '20 18:06 scanny

@scanny makes a good point. That's an important consideration. If you want to be able to edit the content created have to use placeholders in your layout. As @scanny pointed out regular shapes will be read-only.

CaptKludge avatar Jun 02 '20 13:06 CaptKludge

@scanny is it possible to merge all the output pptx files I am creating into a final presentation in a certain order?

avranil26 avatar Jun 06 '20 14:06 avranil26

Many years later, I am trying to do the same thing, duplicate slides that act as templates.

The solution I have currently is to generate one pptx file by slide I want to generate (by loading the templating presentation once for each slide, and delete all slides but the target template one). Afterwards, I am able to combine all slides into a single PDF presentation by

  • using Gotenberg to go from PPTX to PDF for each pptx file
  • using pypdf to merge all resulting PDF files into a single one

This allows me to get a PDF but not a PPTX presentation. I am currently trying to implement slide duplication, and it's quite tedious to get it right...

AurelienRibon avatar Nov 26 '24 16:11 AurelienRibon