docx
它能做什么
这个技能让 AI 智能体处理 Microsoft Word 文档。适合需要把报告、信件或备忘录做成 Word 文件,或需要阅读和编辑现有 Word 文件的人。
适用场景
- ✓当你需要把报告、信件或备忘录制作成 .docx 文件的时候。
- ✓当你想从现有 Word 文件中提取文字或表格的时候。
- ✓当你需要为 Word 文档添加修订记录或批注的时候。
技能链接
https://github.com/HKUDS/DeepTutor/blob/main/deeptutor/skills/builtin/docx/SKILL.md复制后粘贴给你的 AI(Claude、ChatGPT 或 Cursor),让它按照说明操作。
使用方法
最简单的方法:复制下面的消息,粘贴给你的 AI。它会读取这个技能并按说明操作。
可直接粘贴的消息
适用于 Claude Code、Codex 或 Cursor:在项目文件夹中运行,即可安装该技能。
下载 SKILL.md,放进智能体的技能文件夹。
可以这样问 AI
“请把这些笔记整理成一份 Word 报告,包含标题、一个表格和页码。”
每周 5 个新技能,1 分钟读完。
工作原理
智能体在大多数任务中使用 python-docx 库;对于修订记录和批注等高级情况,它会直接编写原始 Word XML。它保存的是新的输出文件,而不会覆盖原始文件。
技术内容(原始 SKILL.md)
作者在 GitHub 上的英文原文。
DOCX (Microsoft Word)
A .docx is a ZIP of XML parts. Body text lives in word/document.xml. Two tiers:
- Default —
python-docx(preinstalled): create, read, and simple edits. Use for almost everything. - Advanced — raw OOXML via
zipfile: only for what python-docx cannot express — tracked changes (redlines), comments, and exact-fidelity edits that must preserve every untouched byte. See Raw OOXML.
Runtime
Use exec with complete Python source (language: python). python-docx is
preinstalled. Prefer creating, saving, reopening, and validating the deliverable
in one call; later calls can revise the same relative filename. Follow the
turn's User workspace instructions for locating inputs, output boundaries,
and presenting the finished file. When editing, write a new output unless the
user explicitly requested an authorized replacement.
Read / extract
from docx import Document
doc = Document("in.docx")
text = "\n".join(p.text for p in doc.paragraphs) # body paragraphs
for tbl in doc.tables: # tables
for row in tbl.rows:
print([c.text for c in row.cells])
doc.paragraphs skips text inside tables, headers/footers, and text boxes — iterate doc.tables and doc.sections[i].header/.footer for those. Each paragraph's style: p.style.name (e.g. "Heading 1").
To read tracked changes, parse the XML directly — python-docx ignores <w:ins>/<w:del>:
import zipfile, re
xml = zipfile.ZipFile("in.docx").read("word/document.xml").decode("utf-8")
# inserted text = <w:ins>…<w:t>… deleted = <w:del>…<w:delText>…
print(re.findall(r"<w:t[^>]*>(.*?)</w:t>", xml))
Create
from docx import Document
from docx.shared import Pt, Inches, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
doc = Document() # default template page size
doc.add_heading("Quarterly Report", level=0) # 0 = title; 1..9 = H1..H9
p = doc.add_paragraph("Intro paragraph. ")
run = p.add_run("Bold tail.")
run.bold = True
doc.add_paragraph("First item", style="List Bullet") # real list style, never a "• " literal
doc.add_paragraph("Step one", style="List Number")
# Table — header row + data
tbl = doc.add_table(rows=1, cols=2)
tbl.style = "Light Grid Accent 1"
tbl.rows[0].cells[0].text, tbl.rows[0].cells[1].text = "Metric", "Value"
for k, v in [("Revenue", "1.2M"), ("Growth", "15%")]:
c = tbl.add_row().cells
c[0].text, c[1].text = k, v
doc.add_picture("chart.png", width=Inches(5)) # image, scaled to width
doc.add_page_break()
doc.save("out.docx")
# Validate immediately; later exec calls can also reopen this relative path.
check = Document("out.docx")
assert check.paragraphs, "generated DOCX has no paragraphs"
import zipfile
with zipfile.ZipFile("out.docx") as package:
assert package.testzip() is None, "generated DOCX has a corrupt ZIP member"
Rules:
- Never type bullet/number characters (
•,1.) into text — usestyle="List Bullet"/"List Number". Only list styles defined in the doc's template are available. - No
\ninside a run — each visual line is its ownadd_paragraph. - Built-in style names must match the template (e.g.
"Heading 1","List Bullet"); a wrong name raisesKeyError. - Units:
Pt,Inches,Cmfromdocx.shared. Colors:RGBColor(0x1F,0x4E,0x79).
Page setup, headers/footers, page numbers
from docx.shared import Inches
sec = doc.sections[0]
sec.page_width, sec.page_height = Inches(8.5), Inches(11) # Letter
sec.top_margin = sec.bottom_margin = Inches(1)
sec.header.paragraphs[0].text = "Confidential"
Page-number fields aren't in the python-docx API; inject the field XML into a footer run:
from docx.oxml.ns import qn
from docx.oxml import OxmlElement
def add_page_number(paragraph):
for t in ("begin", "instr", "end"):
r = OxmlElement("w:r")
if t == "instr":
fld = OxmlElement("w:instrText")
fld.set(qn("xml:space"), "preserve")
fld.text = "PAGE"
else:
fld = OxmlElement("w:fldChar")
fld.set(qn("w:fldCharType"), t)
r.append(fld)
paragraph._p.append(r)
add_page_number(doc.sections[0].footer.paragraphs[0])
A clickable Table of Contents is also a field; Word shows "right-click → Update Field" until refreshed. Same pattern with instrText = TOC \o "1-3" \h \z \u.
Edit existing (simple)
python-docx preserves the rest of the doc
常见问题
它能处理 PDF 或 Excel 文件吗?
不能。该技能只针对 Word 文件,说明中提到 PDF、Excel、PowerPoint 和 Google Docs 不在其范围之内。
它可以添加目录吗?
可以。该技能涵盖目录和页码。
它可以做修订记录吗?
可以。它可以通过高级方法应用修订记录和批注。
详情
作者的原始描述
Read, create, or edit Microsoft Word .docx files — extract/summarize text and tables, generate reports/letters/memos with headings, tables, images, TOC and page numbers, do find-and-replace, or apply tracked changes (redlines) and comments. Use whenever the user has a .docx or wants a Word deliverable. Not for PDF, .xlsx, .pptx, or Google Docs.
repo: HKUDS/DeepTutor
热度
- GitHub 星标
- 54k
- 排名
- #3368
- 许可证
- Apache-2.0
- 更新时间
- 2026/10/31
评论
- …