我正在尝试在 PDF 中搜索字符串并突出显示它们并使用 Python 保存它。数据文件是一个 excel 表(第 2 列),并且还包含特殊字符。我尝试为此使用 PyMuPDF lib,但它给出了以下错误:“
下面是使用的代码:
#pip install pymupdf
#Library to interact with pdfs
import fitz
import time
#pip install xlrd
#lib to read excel files
#import xlrd
#Opening the excel file and the specified sheet. Excel File path to be supplied here
wb = xlrd.open_workbook('C:\\Users\\xyz\\Desktop\\PDF Property File.xlsx')
sh = wb.sheet_by_index(0)
#Read data from column:
value_list=sh.col_values(1, start_rowx=1)
### READ IN PDF
## Give the pdf file path here
doc = fitz.open(r'C:\\Users\\xyz\Desktop\\Test Demo--All Filled.pdf')
page = doc[0]
##IO operaiton
import os
for page in doc:
for i in value_list:
#print(i)
text_instances = page.searchFor(i)
timestr = time.strftime("%Y%m%d-%H%M%S")
for inst in text_instances:
highlight = page.addHighlightAnnot(inst)
doc.save(r"C:\Users\xyz\Desktop\Output\PDF"+ timestr +".pdf" , garbage=4, deflate=True, clean=True)
os.system(r'C:\Users\xyz\Desktop\Output\PDF'+ timestr +".pdf")