-2

I am writing a little program that allows the user to open a pdf file, then the program adds image 1 to pages that contain text 1, image 2 to pages that contain text 2, and save the PDF file. But I kept getting this error "str' object has no attribute 'getNumPages" Below is what I have so far

import fitz
import PyPDF2
import re

main_win.sourceFile = filedialog.askopenfilename(parent=main_win, initialdir= "/",      title='Please select the PDF file for conversion')

document = fitz.open(main_win.sourceFile)
NumPages = main_win.sourceFile.getNumPages()
page = document
String1 = "text1"
String2 = "text2"
dst_pdf_filename = 'destination.pdf'
img_filename1 = 'image1'
img_filename2 = 'image2'
img_rect = fitz.Rect(55, 28, 180, 390)

for i in range(0, NumPages):
 PageObj = main_win.sourceFile.getPage(i)
 Text = PageObj.extractText()
 if re.search(String,Text):
    page.insertImage(img_rect, filename=img_filename1)
    
document.save(dst_pdf_filename)
document.close()
Zac
  • 13
  • 1
  • 5
  • It appears that `askopenfilename` returns a **string**, which you assign to `main_win.sourceFile`. Then you try and access `main_win.sourceFile.getNumPages()`, a method that does not exist on a string. – khelwood Jan 30 '22 at 16:53

1 Answers1

1

In this line: NumPages = main_win.sourceFile.getNumPages()

main_win.sourceFile is a string containing the file name. getNumPages() is not a string method.
You should call the method on the document variable, the document should have pages, not a string.

Shiv
  • 370
  • 1
  • 14
  • Okay so change this line 'NumPages = main_win.sourceFile.getNumPages()'. to 'NumPages = document.getNumPages()' ? – Zac Jan 30 '22 at 17:21
  • Yes, absolutely correct. – Shiv Jan 30 '22 at 18:18
  • Okay, now I get this error "'Document' object has no attribute 'getNumPages'", there must be another basic error that I am not seeing.. @Shib – Zac Jan 30 '22 at 18:56
  • I had actually guessed, maybe look into the documentation of whatever you are using to figure it out. – Shiv Jan 30 '22 at 21:17