Search code examples
python-pptxos.pathnested-for-loop

Iterating through imagis in multiple directories with nested for loops, nth loop repeats last item of prior loop - Python


I was tasked with moving qc pictures of a several freight shipments to ppt slides in a particular format and have been running into an several issues. First, the pictures loaded are not in the same sequence as they are in the directories and subs. This is important as they are saved in the folders in the correct orders. This has also caused them to populate at inverse coordinates in the slides. My second and largest issue is that the first slide has 4 unique images, but every slide there after starts with the same image that was the last one used on previous slide ,ie:

  • slide 1 = img00, img01, img02, img03
  • slide 2 = img03, img04, img05, img06
  • slide 3 = img06, img07, img08, img09

How can I iterate though files and add them to the slides without this duplication at the begging of each loop? I have for something similar to an if i not in condition but this breaks the code. As it stands now, it will create slides and add images but not very well. Some of the other answers to similar questions have addressed how to build better loops, and how to add/drop duplicate items from lists but have not addressed how to iterate over items without touching the same item twice. Any help appreciated.

import os
from os import listdir
from os import walk
from pptx import Presentation
from pptx.util import Inches, Pt
import math



prs = Presentation()
path='c:\\Users\\mainuser\\Pictures\\'

sub_dirs=[]
images=[]

for root,dirname,files in os.walk(path):
    sub_dirs.append(root)
    for file in files:
        if file.endswith('.jpg') or file.endswith('.png'):
            images.append(root+'\\'+file)


def create_title_slide():
    title_slide_layout=prs.slide_layouts[0]
    slide=prs.slides.add_slide(title_slide_layout)
    title=slide.shapes.title
    subtitle=slide.placeholders[1]

    title.text="Presentation Title"
    subtitle.text= "Subtitle text"

    prs.save('test.pptx')

def add_slides_with_pictures():

    blank_slide_layout = prs.slide_layouts[6]

    
    left = Inches(0)
    right= Inches(8)
    top = Inches(0)
    bottom = Inches(3)
    width = Inches(2)
    height = Inches(2)

    
    number_of_slides=round(len(images)/4)
   
                         
    for i in range(0,len(images),1):
        for n in range(number_of_slides):
            
   
            slide= prs.slides.add_slide(blank_slide_layout)

            pic00=slide.shapes.add_picture(images[0], left, top, width, height)

            i+=1
           
            pic01=slide.shapes.add_picture(images[i], right, top,width, height)
           
            i+=1
           
            pic02=slide.shapes.add_picture(images[i], right, bottom, width, height)
           
            i+=1
           
            pic03=slide.shapes.add_picture(images[i], left, bottom, width, height)
           
            i+=1

            prs.save('test.pptx')
            

def main():
    create_title_slide()
    add_slides_with_pictures()
       

Solution

  • Try changing the image loop to the following

    for i in range(0,len(images),4):  # each slide has 4 images
        slide = prs.slides.add_slide(blank_slide_layout)
        pic00 = slide.shapes.add_picture(images[i+0], left, top, width, height)
        pic01 = slide.shapes.add_picture(images[i+1], right, top,width, height)
        pic02 = slide.shapes.add_picture(images[i+2], right, bottom, width, height)
        pic03 = slide.shapes.add_picture(images[i+3], left, bottom, width, height)
    prs.save('test.pptx')