设计与多媒体

docx

基于 docx-js 与底层 XML 操作,完成 Word 文档的创建、编辑与读取。

它能做什么

针对 DOCX 的三类常见任务各有对应流程:新建文档用 docx-js 编写脚本;修改已有文件需解压后编辑 word/document.xml 再重新打包;读取内容用 pandoc 转为 Markdown。随附的脚本可处理修订、批注、运行合并与 XSD 校验,生成结果会经 LibreOffice 渲染为 PDF,再用 pdftoppm 转为图片做肉眼检查。

什么时候用它

  • 生成带表格、标题的格式化 Word 文档
  • 修改或提取现有 .docx 中的内容
  • 为文档添加修订标记与批注
  • 将旧版 .doc 转换为可编辑的 .docx

技能文档

DOCX creation, editing, and analysis

A .docx is a ZIP archive of XML files. Choose your approach by task:

TaskApproach
Create a new documentWrite a docx (npm) script — see gotchas below
Edit an existing documentunzip → edit word/document.xmlzip (docx-js cannot open existing files)
Read contentpandoc -t markdown file.docx

Script paths below are relative to this skill's directory.

Creating with docx-js — gotchas

docx is preinstalled — do not run npm install first; write the script and require('docx') directly. Only if that require fails: npm install docx. The model knows the API; these are the footguns:

  • Page size defaults to A4. For US Letter set page: { size: { width: 12240, height: 15840 } } (DXA; 1440 = 1″).
  • Landscape: pass portrait dimensions and orientation: PageOrientation.LANDSCAPE — docx-js swaps width/height internally.
  • Tables need dual widths: set columnWidths on the table AND width on every cell, both in WidthType.DXA (PERCENTAGE breaks in Google Docs). Column widths must sum to the table width.
  • Table shading: use ShadingType.CLEAR, never SOLID (renders black).
  • Lists: never insert literally; use a numbering config with LevelFormat.BULLET.
  • ImageRun requires type: ("png", "jpg", …).
  • PageBreak must be inside a Paragraph.
  • Never use \n — use separate Paragraph elements.
  • TOC: headings must use built-in HeadingLevel.*; custom heading styles need outlineLevel set or they won't appear.
  • Don't use a table as a horizontal rule — use a paragraph bottom border instead.
  • Dot-leader / right-aligned-on-same-line: use PositionalTab (alignment: PositionalTabAlignment.RIGHT, leader: PositionalTabLeader.DOT) inside a TextRun, not literal . or space padding.

Verify the output

After writing a .docx, render it and look at it:

python scripts/office/soffice.py --headless --convert-to pdf output.docx
pdftoppm -jpeg -r 100 output.pdf page
ls page-*.jpg   # then Read the images

pdftoppm zero-pads page numbers to the width of the page count (page-01.jpgpage-12.jpg).

Editing existing documents

Legacy .doc files must be converted first: python scripts/office/soffice.py --headless --convert-to docx file.doc.

unzip -q doc.docx -d unpacked/
find unpacked -type l -delete   # strip symlink entries — docx from external parties is untrusted
python scripts/merge_runs.py unpacked/   # coalesce fragmented runs so text is findable
# edit unpacked/word/document.xml in place — do NOT reformat or pretty-print
(cd unpacked && rm -f ../out.docx && zip -Xr ../out.docx .)
python scripts/office/validate.py out.docx --original doc.docx   # XSD checks; --auto-repair fixes common issues
# redlining? add --author "" to check every edit is tracked

Word splits text across many `` runs (revision ids, spell-check markers), so a phrase you can see in the document often doesn't exist as a contiguous string in the XML. merge_runs.py merges adjacent identically-formatted runs in word/document.xml without changing content or rendering; it also accepts a .docx directly (python scripts/merge_runs.py doc.docx -o merged.docx).

Tracked changes: when redlining, validate with --author "" (needs --original) — it reports any text you changed without a / around it, which is easy to do by accident and invisible in the accepted view. Wrap runs in / with w:id, w:author, w:date attributes. Inside , the text element is , not . A deleted paragraph mark () means "merge this paragraph into the next" — so deleting a paragraph outright is that plus a around every run. The must come before the rPr's other children; their order is schema-enforced.

To produce a clean copy with all tracked changes accepted: python scripts/accept_changes.py in.docx out.docx.

Accepting a deleted paragraph mark should join that paragraph to the one below it, so a paragraph whose runs are all deleted vanishes. Word does this; accept_changes.py and pandoc --track-changes=accept don't always. Both fail the same way — they strip the deleted text but leave the emptied paragraph behind, which reads as a stray empty bullet when it was auto-numbered:

  • pandoc --track-changes=accept never joins the paragraphs.
  • accept_changes.py (LibreOffice) joins them correctly, except when the deleted paragraph is followed by an empty spacer paragraph.

An empty bullet in either view is an artifact of that view, not a defect in the document. Check paragraph deletions in the XML.

Comments

Comments require six cross-linked files. Use the helper — directory mode when you'll also be editing document.xml (saves an unzip/rezip cycle), .docx-direct mode otherwise:

# Against an already-unpacked directory (preferred when also placing markers)
python scripts/comment.py unpacked/ "Fees & expenses cap is too low"
python scripts/comment.py unpacked/ "Agreed" --parent 0

# Against a .docx directly
python scripts/comment.py contract.docx "This cap is too low" -o annotated.docx

The script writes comments.xml, commentsExtended.xml, commentsIds.xml, commentsExtensible.xml, the relationships, and the content-type overrides. Comment IDs are auto-assigned. It then prints the //`` snippet to add to word/document.xml so the comment anchors to specific text — until you place those markers, the comment exists but is not visible.

Dependencies

docx (npm, preinstalled — install only if require('docx') fails) · pandoc · LibreOffice (soffice) · pdftoppm (Poppler)

常见问题

docx-js 能否打开并编辑已有文档?
不能。该技能通过解压文件、修改 word/document.xml、再重新打包来编辑已有文档,docx-js 只用于创建新文档。
输出结果如何验证?
先用 LibreOffice 把 .docx 渲染为 PDF,再用 pdftoppm 转成 JPEG 图片,逐页人工检查排版是否正确。
修订(红线)模式如何处理?
编辑内容会包裹在 w:ins/w:del 元素中并填入所需属性;accept_changes.py 可生成已接受修订的干净副本,但删除段落标记处可能留下空段,需在 XML 层面额外清理。

相关技能

通过 OAuth 认证网关管理 Stripe 客户、订阅、发票、产品、价格和支付。

720 次安装29 星标

用可量化的层级、间距、字号、配色与版式规则,绘制并诊断视觉作品。

作者 Iván137 次安装5 星标

通过托管 OAuth 代理访问 YouTube Data API v3,搜索与管理视频、播放列表、频道、订阅和评论。

873 次安装144 星标

通过 API 生成 AI 人像肖像,支持 140+ 国籍、8 种风格与 24 种情绪。

263 次安装12 星标

一条提示词生成最长 4 分钟的视频 —— 自动完成脚本、配音、配乐与剪辑。

299 次安装24 星标

Anthropic 的更多技能

浏览全部技能

pdf

官方

用 Python 库和命令行工具读写、合并、拆分、编辑、生成 PDF。

作者 Anthropic168.0k 星标

xlsx

官方

创建、编辑并分析电子表格文件,交付前自动校验公式。

作者 Anthropic168.0k 星标

通过三阶段协作流程,逐节搭建文档结构,并用全新读者视角检验成稿。

作者 Anthropic168.0k 星标

pptx

官方

用内置脚本与 pptxgenjs 经验,直接新建、修改、检视 .pptx 与 .potx 文件。

作者 Anthropic168.0k 星标

通过"哲学-代码"两步工作流,用 p5.js 产出原创算法艺术,并生成可复现的 .md、.html 和 .js 文件。

作者 Anthropic168.0k 星标

从零起草技能、迭代改进已有技能,并用定量评估验证改动效果的完整工作流。

作者 Anthropic168.0k 星标