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