0

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'))
scanny
  • 26,423
  • 5
  • 54
  • 80
Python Bang
  • 472
  • 3
  • 17
  • slide = prs.slides[5] #This is used for 5th slide. You have to use this. Does it work ? – mozilla-firefox Feb 04 '20 at 07:09
  • anan_143 I have tried it earlier it throws error: raise IndexError("slide index out of range") IndexError: slide index out of range – Python Bang Feb 04 '20 at 08:59
  • It should have worked. Have you tried ignoring the first 4 slides ? Because you have to create that as well. That maybe a reason for Slide index out of range. – mozilla-firefox Feb 04 '20 at 09:05
  • `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) slide = prs.slides[5] left = top = Inches(1) pic = slide.shapes.add_picture(imagePath, left, top) prs.save(os.path.join(root,folder1,'PPT_plots.pptx'))` This is what i have tried – Python Bang Feb 04 '20 at 09:07

2 Answers2

1

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

Anupam Chaplot
  • 1,134
  • 1
  • 9
  • 22
  • I am creating new ppt with images from another folder not in an existing ppt. I want to start inserting images from 5th slide instead of first slide that is default – Python Bang Feb 04 '20 at 10:35
  • What I understood is that you want to create a new ppt, where you want to insert the images from 5th slide. I have updated my answer above. – Anupam Chaplot Feb 04 '20 at 11:02
1

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'))
scanny
  • 26,423
  • 5
  • 54
  • 80