使用Python在PDF中查找字符串并高亮显示。

问题描述 投票:0回答:1

我试图在PDF中搜索字符串,并使用Python高亮显示并保存。数据文件是一个excel表(第2列),也包含特殊字符。我试着使用PyMuPDF库,但它给出了以下错误:"enter image description here

下面是使用的代码。

#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")
python-3.x pdf highlight pymupdf
1个回答
0
投票

对你的错误的解释。数据文件中的 value_list 是没有字符串.我不知道xlrd包,所以我不能给建议如何改变......。

© www.soinside.com 2019 - 2024. All rights reserved.