0

Sorry if it is at all confusing I am very new to python and am trying to get my foot into the industry by automating simple tasks at the company I work for. This is a for loop designed to pull a specifically labeled page out of a pdf page matching the file path. Everything works fine until the code loops more than twice it gets stuck on the second file, it opens and re runs the second for loop over again instead of iterating again through the first and grabbing the third pdf file. It seems like regardless of the number of loops it gets stuck on the second one. Any help would be greatly appreciated. (the print functions were purely for troubleshooting)

cert_location = 'G:\Materials Received\CERTS SENT\Leave_Empty_Cert_Puller\\'
filepath = df["filename"].tolist()
heatnumber= df["HeatNumber"].tolist()
certname = df["Job"].tolist()
job = str(len(heatnumber))

print(filepath)
print(heatnumber)
print(certname)

i=0
for row in certname:   
    
    doc=fitz.open(filepath[i])
    page_number = 0
    j=0
    
    for Page in doc:
        
        page_name = Page.get_label()
        page = doc.load_page(j)
        pix = page.get_pixmap()

        if page_name == heatnumber[i]:
            
            pix.save(cert_location + certname[i] + " Heat " + heatnumber[i] +  ".png")
            print("we got one")
        print(Page)    
        print ("missed it")
        j += 1
    print(row)   
    print("next Job")
    i=+1
  • Hi there, I can't understand the problem at al but here some coments on things that could contribute to the problem. 1) usually when you open a file you need to close it somewhere, this is tricky but's it's important to not damage files and avoid some problems. (check context manager opening). 2) j appears to increment once every loop in the inner one, use `for j,Page in enumerate(doc):` instead. same you could do in the outer loop with `i` – Ulises Bussi Jun 09 '22 at 18:57
  • 2
    it is a typo, at the end, you need `i += 1` and not `i = +1` as you wrote :) – Ben.T Jun 09 '22 at 18:58
  • 1
    i see the problem, you put `i=+1` instead `i+=1` so it never take other values than 0 or 1 – Ulises Bussi Jun 09 '22 at 18:59
  • Thank You all for the += I have been banging my head off a desk for 2 hours and I knew it would be something simple. – George Gehring Jun 09 '22 at 19:14

1 Answers1

0

Its definitely the typo at the end when incrementing 'i' for the outer loop

i += 1
and not
i=+1