I am trying to search strings in PDF and highlight them and save it using Python. The data file is an excel sheet(column 2) and contains special characters as well. I tried using PyMuPDF lib for this but its giving the below error:
"
Below is the code used:
#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")