设计与多媒体

docx

试用

用脚本和 XML 创建、编辑、读取并校验 Word 的 .docx 与 .dotx 文件。

它能做什么

按任务分三条路径:新建文档用 docx-js 写脚本生成;修改已有文档则解包 ZIP、直接改 word/document.xml 再重新打包(docx-js 打不开已有文件);读取内容用 pandoc 转成 markdown。随附脚本可以合并被拆碎的 run、写入批注所需的六个互相关联的文件、接受修订,并用 XSD 校验输出。生成的文件会转成 PDF 再渲染成图片,方便逐页检查排版。

什么时候用它

  • 生成带标题层级、表格和目录的正式文档
  • 用修订和批注对合同做红线标注
  • 从别人发来的 .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 documentunpack → edit word/document.xml → zip (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.
  • Headings: style every level you use (title, heading1, heading2…) in styles.default: heading1: { run: { font: "Arial", size: 32, bold: true }, paragraph: { spacing: { before: 240, after: 120 }, keepNext: true } }. Every level needs its own font (even with a document default), before and after (even if 0) and keepNext: the built-in styles set none of them.
  • 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:

zip -d output.docx '*/' >/dev/null || true   # drop the ZIP's folder entries: Word has refused a file that has them when opened through a cloud co-editing service
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.jpg…page-12.jpg).

Editing existing documents

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

python3 -c "import sys,zipfile; zipfile.ZipFile(sys.argv[1]).extractall('unpacked')" doc.docx   # not unzip, which restores symlinks — 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 -XrDy ../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,它无法打开已有文件。做法是解包 .docx、就地修改 word/document.xml,然后重新打包。
为什么 Word 里看得到的文字在 XML 里搜不到?
Word 会因修订 id、拼写检查标记把文字拆成很多 run。先跑 merge_runs.py,它会把相邻且格式相同的 run 合并,不改变内容和渲染效果。
怎么确认生成的文件排版没问题?
技能会用 LibreOffice 把 .docx 转成 PDF,再用 pdftoppm 渲染成 JPEG,可以逐页查看。另外提供 validate.py 做 XSD 校验,--auto-repair 可修复常见问题。

相关技能

以 AI 机器人身份加入视频会议,提供语音、虚拟形象与屏幕共享四种模式。

作者 johnpatternai22 次安装8 星标

pdf

官方

用 Python 库和命令行工具读取、提取、合并、拆分、旋转、加水印、加密和生成 PDF。

作者 Anthropic197.6k 星标

搭建、重构和排查由 MCP 服务器加 widget 界面组成的 ChatGPT Apps SDK 应用。

作者 OpenAI28.8k 星标

用已发布的设计系统组件、变量和样式,把代码或描述转成完整的 Figma 页面并持续更新。

作者 OpenAI28.8k 星标

把代码库里的设计系统按阶段搬进 Figma,每个阶段停下来等你确认。

作者 OpenAI28.8k 星标

Anthropic 的更多技能

浏览全部技能

用「收集背景 → 逐节打磨 → 读者测试」三个阶段,把零散想法写成一份能给别人看的文档。

作者 Anthropic197.6k 星标

pdf

官方

用 Python 库和命令行工具读取、提取、合并、拆分、旋转、加水印、加密和生成 PDF。

作者 Anthropic197.6k 星标

pptx

官方

用 pptxgenjs 脚本、XML 编辑和校验流程创建、修改和读取 .pptx / .potx 文件。

作者 Anthropic197.6k 星标

xlsx

官方

生成、修改并校验带真实公式的 Excel 工作簿,保证数字格式正确、公式零报错。

作者 Anthropic197.6k 星标

把一句生成艺术需求变成一份算法哲学文稿,外加可直接运行的 p5.js 交互页面。

作者 Anthropic197.6k 星标

从需求梳理到草稿、测试、迭代,帮你把一个 agent skill 做出来并验证效果。

作者 Anthropic197.6k 星标