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.xml。comment() 支持可选参数 date(默认当前系统时间)。
同一 DocxDocument 可在多线程里使用;多个文档各 parse 一份再并行即可。
更多 API 说明见仓库 README(英文)与 README_zh-CN(中文)。