Back to skills
SKILL.md
Docx
ASecurity在用户需要创建、读取、编辑或处理 Word 文档(.docx)或模板(.dotx)时使用。触发情形包括提到 Word、.docx 或 .dotx,要求交付带目录、标题、页码或信头的正式文档,以及提取或重排内容、插入或替换图片、查找替换、编辑修订或批注。用户要求交付 Word 版报告、备忘录、信函或模板时也适用。PDF、电子表格、Google Docs 和与文档生成无关的编程任务使用各自的技能。
- 626 stars
- 0 votes
- 0 copies
- 9 views
- Added February 10, 2026
Security analysis
92/100- Installs packages at runtime which could introduce malicious dependencies
Pro scans all 19 files and shows the line behind each finding
npx -y skills add LeastBit/Claude_skills_zh-CN --skill docx --agent claude-codeAre you the author of Docx?
Add the live security badge to your README. It updates with every re-scan.
[](https://www.skillsdirectory.com/skills/leastbit-docx)---
name: docx
description: "在用户需要创建、读取、编辑或处理 Word 文档(.docx)或模板(.dotx)时使用。触发情形包括提到 Word、.docx 或 .dotx,要求交付带目录、标题、页码或信头的正式文档,以及提取或重排内容、插入或替换图片、查找替换、编辑修订或批注。用户要求交付 Word 版报告、备忘录、信函或模板时也适用。PDF、电子表格、Google Docs 和与文档生成无关的编程任务使用各自的技能。"
license: 完整条款见 LICENSE.txt
---
# 创建、编辑和分析 DOCX
`.docx` 是由若干 XML 文件打包而成的 ZIP 归档。按任务选择处理方式。
| 任务 | 处理方式 |
|---|---|
| 创建文档 | 用 npm 的 `docx` 编写脚本,参考下文的注意事项 |
| 编辑现有文档 | 解包、编辑 `word/document.xml`、用 `zip` 重新打包。docx-js 无法打开现有文件 |
| 读取内容 | `pandoc -t markdown file.docx` |
> 下文的脚本路径均相对于本技能目录。
## 使用 docx-js 创建文档
环境预装了 `docx`,直接编写脚本并调用 `require('docx')`。加载失败时才运行 `npm install docx`。
- **默认纸张为 A4。** US Letter 使用 `page: { size: { width: 12240, height: 15840 } }`。尺寸单位为 DXA,1 英寸等于 1440 DXA。
- **横向页面**仍传入纵向尺寸,再设为 `PageOrientation.LANDSCAPE`,docx-js 会在内部互换宽和高。
- **表格宽度要设两处。** 设置整张表的 `columnWidths` 和每个单元格的 `width`,都用 `WidthType.DXA`。Google Docs 不能正确处理 `PERCENTAGE`。各列宽度之和应等于表宽。
- **表格底纹**使用 `ShadingType.CLEAR`,`SOLID` 会渲染成黑色。
- **项目符号列表**通过 `numbering` 配置 `LevelFormat.BULLET`。
- **`ImageRun`** 必须指定 `type`,例如 `"png"` 或 `"jpg"`。
- **分页**时把 `PageBreak` 放在 `Paragraph` 内。需要另起一段时创建新的 `Paragraph`。
- **标题样式**在 `styles.default` 中为每一级实际使用的标题单独配置,例如 `heading1: { run: { font: "Arial", size: 32, bold: true }, paragraph: { spacing: { before: 240, after: 120 }, keepNext: true } }`。即使已有文档级默认字体,每级也要明确指定字体、段前段后距离(为 0 也要写)和 `keepNext`。
- **目录**采用内置的 `HeadingLevel.*`。自定义标题样式需要设置 `outlineLevel` 才能进入目录。
- **水平分隔线**用段落下边框实现。
- **同一行内右对齐或添加点引导线**时,在 `TextRun` 中使用 `PositionalTab`,设置 `alignment: PositionalTabAlignment.RIGHT` 及 `leader: PositionalTabLeader.DOT`。
## 检查输出
生成 `.docx` 后渲染并逐页查看。
```bash
zip -d output.docx '*/' >/dev/null || true # 移除 ZIP 中的目录条目,避免 Word 从云端协作服务打开时拒绝加载
python scripts/office/soffice.py --headless --convert-to pdf output.docx
pdftoppm -jpeg -r 100 output.pdf page
ls page-*.jpg # 查看生成的页面图像
```
`pdftoppm` 会按总页数为页码补零,例如 `page-01.jpg` 至 `page-12.jpg`。
## 编辑现有文档
处理旧版 `.doc` 前先转换,运行 `python scripts/office/soffice.py --headless --convert-to docx file.doc`。
```bash
python3 -c "import sys,zipfile; zipfile.ZipFile(sys.argv[1]).extractall('unpacked')" doc.docx # 外部 DOCX 视为不受信任的输入。用 zipfile 解包,避免 unzip 恢复符号链接
python scripts/merge_runs.py unpacked/ # 合并碎片化的 run,使文本可搜索
# 直接编辑 unpacked/word/document.xml,保留原排版和缩进
(cd unpacked && rm -f ../out.docx && zip -XrDy ../out.docx .)
python scripts/office/validate.py out.docx --original doc.docx # 执行 XSD 校验。--auto-repair 可修复常见问题
# 若要校验修订,加 --author "<the name you redlined under>",确认每次编辑都有修订标记
```
Word 会因修订 ID、拼写检查标记等原因,把文字拆成多个 `<w:r>`(run)。页面上连贯的句子在 XML 中可能被拆开,无法直接查找。`merge_runs.py` 会合并 `word/document.xml` 中格式完全相同的相邻 run,保留内容和渲染效果。也可直接输入 `.docx`,例如 `python scripts/merge_runs.py doc.docx -o merged.docx`。
**修订。** 用 `--author "<the name you redlined under>"` 校验修订(须同时提供 `--original`)。校验会指出未包在 `<w:ins>` 或 `<w:del>` 中的文本修改,这类遗漏在接受修订后的视图中不易察觉。用带有 `w:id`、`w:author` 和 `w:date` 的 `<w:ins>` 或 `<w:del>` 包裹调整的 run。`<w:del>` 中的文本用 `<w:delText>` 表示。删除段落标记 `<w:pPr><w:rPr><w:del w:id=".." w:author=".." w:date=".."/></w:rPr></w:pPr>` 表示将该段并入下一段。整段删除时还须用 `<w:del>` 包裹每个 run。`<w:del/>` 必须排在 `rPr` 的其他子元素前面,XML Schema 对此顺序有要求。
运行 `python scripts/accept_changes.py in.docx out.docx` 可生成接受所有修订的副本。
Word 接受删除段落标记的修订后,会把该段并入下一段,因此 run 全部被删除的段落会消失。其他工具可能只删除文字,留下空段。若原段使用自动编号,就会看到一个空项目符号。
- `pandoc --track-changes=accept` 始终保留独立段落。
- `accept_changes.py`(LibreOffice)通常能正确合并,例外是后面紧接一个用于留白的空段。
出现空项目符号时,到 XML 中核对段落删除标记,区分查看工具的显示差异与文档本身的结构问题。
## 批注
批注涉及六个互相关联的文件。若还要改 `document.xml`,对已解包目录运行辅助脚本,可以省去一次解包和打包。只加批注时可直接处理 `.docx`。
```bash
# 处理已解包的目录,也要放置批注标记时优先采用
python scripts/comment.py unpacked/ "Fees & expenses cap is too low"
python scripts/comment.py unpacked/ "Agreed" --parent 0
# 直接处理 .docx 文件
python scripts/comment.py contract.docx "This cap is too low" -o annotated.docx
```
脚本会写入 `comments.xml`、`commentsExtended.xml`、`commentsIds.xml`、`commentsExtensible.xml`,并更新关系和内容类型声明。批注 ID 自动分配。脚本随后打印 `<w:commentRangeStart>`、`<w:commentRangeEnd>` 和 `<w:commentReference>` 片段。将其放入 `word/document.xml` 的指定位置后,批注才会与文本关联并显示。
## 依赖
npm 包 `docx`(预装,加载失败才安装)、`pandoc`、LibreOffice(`soffice`)、Poppler 的 `pdftoppm`。
<!-- 本文件基于 Anthropic Skills 进行简体中文翻译及适配。 -->
Files in this skill
- LICENSE.txt
- SKILL.md
- docx-js.md
- ooxml.md
- ooxml/schemas/ISO-IEC29500-4_2016/dml-chart.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/dml-chartDrawing.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/dml-diagram.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/dml-lockedCanvas.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/dml-picture.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/dml-spreadsheetDrawing.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/dml-wordprocessingDrawing.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/pml.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/shared-additionalCharacteristics.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/shared-bibliography.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/shared-commonSimpleTypes.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlDataProperties.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/shared-customXmlSchemaProperties.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesCustom.xsd
- ooxml/schemas/ISO-IEC29500-4_2016/shared-documentPropertiesExtended.xsd
Attribution
Related skills
View all in development →Comments
Loading comments…
Newsletter
Get the weekly list of safe new skills, and the ones we pulled.