赞
踩
import fitz
file_name = r"e:/test.pdf"
# 注意打开文本和保存文本均为一个文件,最好做好原数据备份
doc = fitz.open(file_name )
page=doc[0]
# 方法一:按照指定的位置设置颜色
highlight = page.add_highlight_annot((20, 500,60, 520)) # 左上,左下,右上,右下
highlight.set_colors(stroke=[1, 1, 0]) # light red color (r, g, b) 颜色rgb每个除以255得出
highlight.update()
# 方法二:按照查找的文本设置颜色
text="2024 年是龙年"
text_instances = page.search_for(text,quads=True)
for inst in text_instances:
highlight = page.add_highlight_annot(inst)
highlight.update()
doc.save(file_name, incremental=True, encryption=0)
效果如图:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。