0

I want to write a simple program that asks the user to open a PDF file from any location, add image A to any page that contains the keywords "Orange County", and add image B to any page that contains the keywords "Hillsborough county", then save the new file at the same location as the old PDF file.

Below is what I have so far, but I kept getting the error "AttributeError: 'Document' object has no attribute 'searchFor'"

main_win.sourceFile = filedialog.askopenfilename(parent=main_win, initialdir= "/", title='Please select the PDF file for conversion')
  
document = fitz.open(main_win.sourceFile)
page = document
if main_win.sourceFile:
    dst_pdf_filename = 'destination.pdf'
    img_filename1 = 'Orange county stamp.png'
    img_filename2 = 'Hillsborough county stamp.png'
    img_rect = fitz.Rect(55, 28, 180, 390)

text = page.searchFor("")
for page in main_win.sourceFile:
    if text == "Orange county":
        page.insertImage(img_rect, filename=img_filename1)

    if text == "Hillsborough county":
        page.insertImage(img_rect, filename=img_filename2)
    
document.save(dst_pdf_filename)
document.close()
Zac
  • 13
  • 1
  • 5
  • @K J Thanks KJ, you are correct, some of the files will only have 1 page. So how would you define page instead? – Zac Jan 30 '22 at 03:08
  • Okay, I will do that, when I get this to work, I will post the new code. Thanks KJ @K J – Zac Jan 30 '22 at 13:01

1 Answers1

1

I found syntax errors here:

if text = "Orange county"
page.insertImage(img_rect, filename=img_filename1)

if text = "Hillsborough county"
page.insertImage(img_rect, filename=img_filename2)
...

Change to this and see if it works :) :

if text == "Orange county":
    page.insertImage(img_rect, filename=img_filename1)

if text == "Hillsborough county":
    page.insertImage(img_rect, filename=img_filename2)
Anynamer
  • 334
  • 1
  • 6
  • Thank you, that did fix one of the issues, now I am getting an error "UnboundLocalError: local variable 'page' referenced before assignment", do you know why? – Zac Jan 29 '22 at 20:35