如何解决如何使用 Python 突出显示 PDF 上的文本?
我正在尝试制作一个允许用户输入 PDF 的 python 脚本,然后用户将输入要搜索的单词,如果找到这些单词,则突出显示并导出为唯一的文件名。如果找不到单词,我有代码会运行,但是由于某种原因,当找到单词时,此代码会中断。任何帮助或建议表示赞赏!
### 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)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。