python - 更改pdf中文本的字体高亮颜色(fitz模块)

python 复制代码
import fitz

doc = fitz.open(r"e:/test.pdf")
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(r"e:/test.pdf",incremental=True,encryption=0)
# 注意打开文本和保存文本为一个文件

效果如图:

相关推荐
Aaron - Wistron12 小时前
Web API C# (Furion版)带 单元测试
开发语言·后端·c#
卷无止境13 小时前
写代码这件事,到底该讲究点什么?
后端·python
卷无止境13 小时前
循环复杂度到底在算什么,Python 代码怎么才能写得让人一看就懂
后端·python
lpfasd12313 小时前
MediaCrawler 项目深度分析
chrome·python·chrome devtools
Dxy123931021613 小时前
Python项目打包成EXE完整教程(PyInstaller实战避坑)
开发语言·python
bamb0014 小时前
一个项目带你入门AI应用开发01
python
05664614 小时前
Python康复训练——常用标准库
开发语言·python·学习
骊城英雄14 小时前
基于C#+avalonia ui实现的跨平台点胶机灌胶监控控制上位机软件
开发语言·ui·c#
吾儿良辰14 小时前
一个被BCL遗忘的高性能集合:C# CircularBuffer<T>深度解析
开发语言·windows·c#
昆曲之源_娄江河畔14 小时前
Python如何安装flask, pymssql
开发语言·python·flask·pymssql