pythonpython-pptx

Placeholder for pictures in python-pptx


I want to make a presentation with python-pptx with 3 pictures in a row on each slide.

from pptx import Presentation
from pptx.util import Inches

height = Inches(3) #size  
top = Inches(0)  #y-position

img = 'image001.png'

prs = Presentation() #create presentation
blank_slide_layout = prs.slide_layouts[6] #define side layout


for j in range(0,5):
    slide = prs.slides.add_slide(blank_slide_layout) #add slide
    left = Inches(0) #x-position
    for i in range(0,3):
        pic = slide.shapes.add_picture(img, left, top, height=height)
        left = left+height    

prs.save('test.pptx')

This here is working but i want to understand the placeholders. I need first to fill the first (left) picture at each slide and jump back to the first slide and all second (mid) pictures and the same for the third column because i am loading the pics from a txt-file.

The txt file look like

pic1a
pic2a
pic3a
pic1b
pic2b
...

and i need the pics in the presentation like

pic1a pic1b pic1c
pic2a..
pic3a...

How can i add placeholders and jump to them. The manual is not helping me.


Solution

  • Add your (blank) slides first, then repeatedly traverse them to add the pictures.

    Roughly like:

    for _ in range(3):
        slides.add_slide(blank_slide_layout)
    
    left = first_left
    for i in range(3):
        pic = slide[i].shapes.add_picture(img, left, top, height=height)
        ...
    
    left = middle_left
    for i in range(3):
        pic = slide[i].shapes.add_picture(img, left, top, height=height)
        ...
    
    left = last_left
    for i in range(3):
        pic = slide[i].shapes.add_picture(img, left, top, height=height)
        ...
    

    If you're so inclined you can do this second part more compactly with a nested loop, but it might be sensible to get it working as three separate clauses first so you can see clearly what needs to change on each of the nested iterations.