【Python】docxnote:优雅的 Word 批注

docxnote 是一个轻量级的 DOCX 批注 库,只依赖 lxml。它直接处理 WordprocessingML,把 docx 当成 ZIP + XML,对外提供 按段落字符串 操作的 API------不暴露 Word 里的 Run,批注用字符区间 start / end 标定。

如果你做过合同/标书/论文的批量审阅,多半踩过这样的坑:用通用 DOCX 库时要自己拆 Run 、对 XML 锚点、维护 comments.xml 和关系------样板代码长、边界情况多,一不留神批注就错位或损坏原文。

安装:

bash 复制代码
pip install docxnote
python 复制代码
from docxnote import DocxDocument, Paragraph, Table

with open("document.docx", "rb") as f:
    doc = DocxDocument.parse(f.read())

for block in doc.blocks():
    if isinstance(block, Paragraph) and block.text:
        block.comment("请检查表述", end=5, author="reviewer")
    elif isinstance(block, Table):
        rows, cols = block.shape()
        for r in range(rows):
            for c in range(cols):
                for inner in block[r, c].blocks():
                    if isinstance(inner, Paragraph) and inner.text:
                        inner.comment("需复核", end=3, author="reviewer")

with open("output.docx", "wb") as f:
    f.write(doc.render())

需要保留文件里已有批注时,使用 DocxDocument.parse(..., keep_comments=True)。批注在调用 render() 时写入 comments.xmlcomment() 支持可选参数 date(默认当前系统时间)。

同一 DocxDocument 可在多线程里使用;多个文档各 parse 一份再并行即可。

更多 API 说明见仓库 README(英文)与 README_zh-CN(中文)。

相关推荐
SelectDB1 天前
Apache Doris Python UDF:让 SQL 直接调用 Python 生态,支撑 Agent 时代复杂业务逻辑
大数据·数据库·python
荣码2 天前
GraphRAG:普通RAG只能回答"点"的问题,我踩了4个坑才搞懂
java·python
金銀銅鐵2 天前
[Python] 基于欧几里得算法,实现分数约分计算器
python·数学
Lyn_Li2 天前
Kaggle Top 5 | 198只股票、200条数据的金融预测——BattleFin高分方案从零复现
python·kaggle·比赛复盘·金融预测
小九九的爸爸3 天前
前端想要入门Agent开发,要具备哪些Python基础?
python·agent·ai编程
阿耶同学3 天前
手把手教你用 LangGraph 搭建三层嵌套 Agent 架构
python·程序员
花酒锄作田3 天前
Pydantic校验配置文件
python
hboot3 天前
AI工程师第四课 - 深度学习入门
pytorch·python·神经网络
ZhengEnCi4 天前
P2M-Matplotlib折线图完全指南-从数据可视化到趋势分析的Python绘图利器
python·matlab·数据可视化
ZhengEnCi4 天前
P2L-Matplotlib饼图完全指南-从数据可视化到图表定制的Python绘图利器
python·matlab