[英]How to highlight multiple keywords in a .pdf file using PyMuPDF library
我可以使用這個突出顯示.pdf 文件中所有出現的單個單詞
但無法突出顯示.pdf 文件中的多個關鍵字。 這是我的代碼
import fitz
import os
keywords = ["remote","setup"]
pdfFile = "\D:\Python_Scripts\Email Analysis\certification-ACDA.pdf"
for keyword in keywords:
pdf = fitz.open(pdfFile)
for page in pdf:
text_instances = page.searchFor(keyword)
if len(text_instances) > 0:
for inst in text_instances:
highlight = page.addHighlightAnnot(inst)
highlight.setColors(stroke=(1, 0, 0))
highlight.update()
pdf.save(f"{os.path.splitext(os.path.basename(pdfFile))[0]}.pdf", garbage=4, deflate=True, clean=True)
完成對單個關鍵字的搜索但收到此錯誤后,我正在覆蓋文件
Traceback (most recent call last): File "D:/Python_Scripts/Email Analysis/PDF.py", line 19, in <module> pdf.save(f"{os.path.splitext(os.path.basename(pdfFile))[0]}.pdf", garbage=4, deflate=True, clean=True) File "D:\Python_Scripts\Email Analysis\venv\lib\site-packages\fitz\fitz.py", line 4209, in save user_pw, RuntimeError: cannot remove file 'certification-ACDA.pdf': Permission denied
請嘗試以下操作:使用“for”迭代字符串
import fitz
pdf_file = fitz.open("filename.pdf")
for page in pdf_file:
mylist=["word1","word2"]
for i in mylist:
text_to_be_highlighted = i
highlight = page.search_for(text_to_be_highlighted)
for inst in highlight:
highlight = page.add_highlight_annot(inst)
highlight.update()
pdf_file.save("output.pdf", garbage=4, deflate=True, clean=True)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.