Reputation: 482
I have a folder with images which I am inserting into a PPTX file I am generating. The below code inserts the images starting with the first slide but I want it to start inserting images from the fifth slide onward. I am unable look for a solution please help. Thanks in advance.
def Generate_PPT(avg_plots):
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
for imagePath in (avg_plots):
if (avg_plots.index(imagePath)) % 2 == 0:
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(imagePath, left, top)
prs.save(os.path.join(root,folder1,'PPT_plots.pptx'))
Upvotes: 0
Views: 1309
Reputation: 28863
One way to approach this is by creating the first four slides before entering your "image insertion" loop:
def Generate_PPT(avg_plots):
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
# --- add four blank slides such that first image appears on fifth slide ---
for _ in range(4):
prs.slides.add_slide(blank_slide_layout)
# --- then image insertion proceeds from fifth slide onward ---
for imagePath in (avg_plots):
if (avg_plots.index(imagePath)) % 2 == 0:
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(imagePath, left, top)
prs.save(os.path.join(root, folder1, 'PPT_plots.pptx'))
Upvotes: 1
Reputation: 1316
Please refer the below link: https://python-pptx.readthedocs.io/en/latest/api/slides.html
To get the fifth slide use get method as shown below.
slide=prs.slides.get(4)
Please refer the below code, if you want to generate a new ppt and add images from 5th slide.
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
for x in range(5):
slide = prs.slides.add_slide(blank_slide_layout) ## 5 slides got created
print(prs.slides.index(slide)) # this will print 4, index for the last slide
slide = prs.slides.get(4) #use this to access slide 5
# do the changes to the slide (add image)
prs.save(os.path.join('PPT_plots.pptx'))
Upvotes: 1