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)
# 注意打开文本和保存文本为一个文件

效果如图:

相关推荐
xxie1237941 分钟前
Python装饰器与语法糖
开发语言·python
郑州光合科技余经理11 分钟前
海外外卖平台源码改造实战——多语言核心代码实现
java·开发语言·前端·后端·mysql·架构·php
不厌 -_-12 分钟前
VSCode 中 Markdown 转 PDF
vscode·pdf
CClaris16 分钟前
大模型量化从0到1(五):GPTQ 原理详解 + 从零量化一个真实大模型
人工智能·python·算法·机器学习
凯瑟琳.奥古斯特20 分钟前
力扣1012数位DP解法详解
开发语言·c++·算法·leetcode·职场和发展
Railshiqian23 分钟前
UserPickerActivity 内部逻辑分析
开发语言·python
一tiao咸鱼30 分钟前
前端转 agent # 02 - FastAPI 框架入门与原理
前端·python
喜欢打篮球的普通人38 分钟前
Trition程序编写:从“Hello CUDA“到“Hello Triton“:向量加法背后的编译黑魔法
开发语言·后端·rust
code_std39 分钟前
java WebSocket 使用
java·开发语言·websocket
gihigo19981 小时前
FastSLAM2.0(精度优于1.0)MATLAB 实现
开发语言·matlab