I am trying to make a python script that allows the user to input a PDF, then the user would input words to be searched for, if those words are found, highlight and exported as a unique file name. I have code that runs if the words aren't found, but for some reason this code is breaking when the words are found. Any help or recommendations are appreciated!
### IMPORT PACKAGES NEEDED
import sys
from inspect import cleandoc
# !pip install PyMuPDF==1.16.14
import fitz
import time
import PySimpleGUI as sg
import sys
searchingWords = []
### READ IN PDF
sg.theme('BlueMono')
inputfname = sg.popup_get_file('PDF Browser', 'PDF file to open', file_types=(("PDF Files", "*.pdf"),))
if inputfname is None:
sg.popup_cancel('Cancelled.')
exit(0)
print(inputfname)
doc = fitz.open(inputfname)
### USER INPUTTING WORDS
# Window definition
layout = [[sg.Text("What word or phrase do you want to search for?")],
[sg.Input(key='-INPUT-', do_not_clear=False)],
[sg.Text(size=(40,1), key='-OUTPUT-')],
[sg.Button('Next word', ), sg.Button('Confirm'), sg.Button('Next word enter', visible=False, bind_return_key=True)]]
# Create the window
window = sg.Window('Word Search', layout)
# Display window
while True:
event, values = window.read()
# See if user wants to quit or window was closed
if event == sg.WINDOW_CLOSED or event == 'Confirm':
break
# Output a message to the window
searchingWords.append(values['-INPUT-'])
window['-OUTPUT-'].update(str(searchingWords))
# Remove window
window.close()
### END USER INPUT FOR SEARCH WORDS
for page in doc:
### SEARCHING FOR THE WORDS
for word in searchingWords:
# ??? How to change this to ensure there is a non-alphabetic letter next to it?
text = str(word)
text_instances = page.searchFor(text)
### HIGHLIGHTING THE WORDS
for inst in text_instances:
highlight = page.addHighlightAnnot(inst)
highlight.update()
### SET FILE OUTPUT NAME
datetimefilename = time.strftime("%m-%d-%Y-%H.%M.%S") + "Highlighted.pdf"
### OUTPUT
doc.save(str(datetimefilename), garbage=4, deflate=True, clean=True)