docx
DOCX 创建、编辑和分析
概述
.docx 文件是一个包含 XML 文件的 ZIP 压缩包。
快速参考
| 任务 | 方法 |
|---|---|
| 读取/分析内容 | pandoc 或解压获取原始 XML |
| 创建新文档 | 使用 docx-js - 参见下方"创建新文档" |
| 编辑现有文档 | 解压 → 编辑 XML → 重新打包 - 参见下方"编辑现有文档" |
转换 .doc 为 .docx
旧版 .doc 文件在编辑前必须转换:
python scripts/office/soffice.py --headless --convert-to docx document.doc
读取内容
# 提取文本(包含修订追踪)
pandoc --track-changes=all document.docx -o output.md
# 访问原始 XML
python scripts/office/unpack.py document.docx unpacked/
转换为图片
python scripts/office/soffice.py --headless --convert-to pdf document.docx
pdftoppm -jpeg -r 150 document.pdf page
接受修订追踪
要生成一个所有修订追踪都已接受的干净文档(需要 LibreOffice):
python scripts/accept_changes.py input.docx output.docx
创建新文档
使用 JavaScript 生成 .docx 文件,然后验证。安装:npm install -g docx
设置
const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell, ImageRun,
Header, Footer, AlignmentType, PageOrientation, LevelFormat, ExternalHyperlink,
InternalHyperlink, Bookmark, FootnoteReferenceRun, PositionalTab,
PositionalTabAlignment, PositionalTabRelativeTo, PositionalTabLeader,
TabStopType, TabStopPosition, Column, SectionType,
TableOfContents, HeadingLevel, BorderStyle, WidthType, ShadingType,
VerticalAlign, PageNumber, PageBreak } = require('docx');
const doc = new Document({ sections: [{ children: [/* content */] }] });
Packer.toBuffer(doc).then(buffer => fs.writeFileSync("doc.docx", buffer));
验证
创建文件后,进行验证。如果验证失败,解压、修复 XML,然后重新打包。
python scripts/office/validate.py doc.docx
页面大小
// 重要:docx-js 默认使用 A4,而非 US Letter
// 始终明确设置页面大小以获得一致的结果
sections: [{
properties: {
page: {
size: {
width: 12240, // 8.5 英寸(DXA 单位)
height: 15840 // 11 英寸(DXA 单位)
},
margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } // 1 英寸边距
}
},
children: [/* content */]
}]
常用页面大小(DXA 单位,1440 DXA = 1 英寸):
| 纸张 | 宽度 | 高度 | 内容宽度(1英寸边距) |
|---|---|---|---|
| US Letter | 12,240 | 15,840 | 9,360 |
| A4(默认) | 11,906 | 16,838 | 9,026 |
**横向方向:**docx-js 会在内部交换宽度/高度,因此传入纵向尺寸,让它处理交换:
size: {
width: 12240, // 将短边作为宽度传入
height: 15840, // 将长边作为高度传入
orientation: PageOrientation.LANDSCAPE // docx-js 在 XML 中交换它们
},
// 内容宽度 = 15840 - 左边距 - 右边距(使用长边)
样式(覆盖内置标题)
使用 Arial 作为默认字体(通用支持)。保持标题为黑色以确保可读性。
const doc = new Document({
styles: {
default: { document: { run: { font: "Arial", size: 24 } } }, // 12pt 默认
paragraphStyles: [
// 重要:使用精确的 ID 来覆盖内置样式
{ id: "Heading1", name: "Heading 1", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { size: 32, bold: true, font: "Arial" },
paragraph: { spacing: { before: 240, after: 240 }, outlineLevel: 0 } }, // outlineLevel 是目录所必需的
{ id: "Heading2", name: "Heading 2", basedOn: "Normal", next: "Normal", quickFormat: true,
run: { size: 28, bold: true, font: "Arial" },
paragraph: { spacing: { before: 180, after: 180 }, outlineLevel: 1 } },
]
},
sections: [{
children: [
new Paragraph({ heading: HeadingLevel.HEADING_1, children: [new TextRun("Title")] }),
]
}]
});
列表(绝不要使用 Unicode 项目符号)
// ❌ 错误 - 绝不要手动插入项目符号字符
new Paragraph({ children: [new TextRun("• Item")] }) // 错误
new Paragraph({ children: [new TextRun("\u2022 Item")] }) // 错误
// ✅ 正确 - 使用 LevelFormat.BULLET 的编号配置
const doc = new Document({
numbering: {
config: [
{ reference: "bullets",
levels: [{ level: 0, format: LevelFormat.BULLET, text: "•", alignment: AlignmentType.LEFT,
style: { paragraph: { indent: { left: 720, hanging: 360 } } } }] },
{ reference: "numbers",
levels: [{ level: 0, format: LevelFormat.DECIMAL, text: "%1.", alignment: AlignmentType.LEFT,
style: { paragraph: { indent: { left: 720, hanging: 360 } } } }] },
]
},
sections: [{
children: [
new Paragraph({ numbering: { reference: "bullets", level: 0 },
children: [new TextRun("Bullet item")] }),
new Paragraph({ numbering: { reference: "numbers", level: 0 },
children: [new TextRun("Numbered item")] }),
]
}]
});
// ⚠️ 每个 reference 创建独立的编号
// 相同 reference = 继续(1,2,3 然后 4,5,6)
// 不同 reference = 重新开始(1,2,3 然后 1,2,3)
表格
关键:表格需要双重宽度设置 - 在表格上设置 columnWidths 并且在每个单元格上设置 width。如果缺少其中任何一个,表格在某些平台上将无法正确渲染。
// 关键:始终设置表格宽度以获得一致的渲染效果
// 关键:使用 ShadingType.CLEAR(而非 SOLID)以防止黑色背景
const border = { style: BorderStyle.SINGLE, size: 1, color: "CCCCCC" };
const borders = { top: border, bottom: border, left: border, right: border };
new Table({
width: { size: 9360, type: WidthType.DXA }, // 始终使用 DXA(百分比在 Google Docs 中会出问题)
columnWidths: [4680, 4680], // 必须等于表格宽度之和(DXA: 1440 = 1 英寸)
rows: [
new TableRow({
children: [
new TableCell({
borders,
width: { size: 4680, type: WidthType.DXA }, // 也要在每个单元格上设置
shading: { fill: "D5E8F0", type: ShadingType.CLEAR }, // CLEAR 而非 SOLID
margins: { top: 80, bottom: 80, left: 120, right: 120 }, // 单元格内边距(内部,不加到宽度上)
children: [new Paragraph({ children: [new TextRun("Cell")] })]
})
]
})
]
})
表格宽度计算:
始终使用 WidthType.DXA — WidthType.PERCENTAGE 在 Google Docs 中会出问题。
// 表格宽度 = columnWidths 之和 = 内容宽度
// US Letter 带 1 英寸边距:12240 - 2880 = 9360 DXA
width: { size: 9360, type: WidthType.DXA },
columnWidths: [7000, 2360] // 必须等于表格宽度之和
宽度规则:
- 始终使用
WidthType.DXA— 绝不要使用WidthType.PERCENTAGE(与 Google Docs 不兼容) - 表格宽度必须等于
columnWidths的总和 - 单元格
width必须匹配对应的columnWidth - 单元格
margins是内部填充 - 它们减少内容区域,而不是增加单元格宽度 - 对于全宽表格:使用内容宽度(页面宽度减去左右边距)
图片
// 关键:type 参数是必需的
new Paragraph({
children: [new ImageRun({
type: "png", // 必需:png, jpg, jpeg, gif, bmp, svg
data: fs.readFileSync("image.png"),
transformation: { width: 200, height: 150 },
altText: { title: "Title", description: "Desc", name: "Name" } // 三个都是必需的
})]
})
分页符
// 关键:PageBreak 必须在 Paragraph 内部
new Paragraph({ children: [new PageBreak()] })
// 或者使用 pageBreakBefore
new Paragraph({ pageBreakBefore: true, children: [new TextRun("New page")] })
超链接
// 外部链接
new Paragraph({
children: [new ExternalHyperlink({
children: [new TextRun({ text: "Click here", style: "Hyperlink" })],
link: "https://example.com",
})]
})
// 内部链接(书签 + 引用)
// 1. 在目标位置创建书签
new Paragraph({ heading: HeadingLevel.HEADING_1, children: [
new Bookmark({ id: "chapter1", children: [new TextRun("Chapter 1")] }),
]})
// 2. 链接到它
new Paragraph({ children: [new InternalHyperlink({
children: [new TextRun({ text: "See Chapter 1", style: "Hyperlink" })],
anchor: "chapter1",
})]})
脚注
const doc = new Document({
footnotes: {
1: { children: [new Paragraph("Source: Annual Report 2024")] },
2: { children: [new Paragraph("See appendix for methodology")] },
},
sections: [{
children: [new Paragraph({
children: [
new TextRun("Revenue grew 15%"),
new FootnoteReferenceRun(1),
new TextRun(" using adjusted metrics"),
new FootnoteReferenceRun(2),
],
})]
}]
});
制表位
// 在同一行右对齐文本(例如,日期与标题相对)
new Paragraph({
children: [
new TextRun("Company Name"),
new TextRun("\tJanuary 2025"),
],
tabStops: [{ type: TabStopType.RIGHT, position: TabStopPosition.MAX }],
})
// 点前导符(例如,目录样式)
new Paragraph({
children: [
new TextRun("Introduction"),
new TextRun({ children: [
new PositionalTab({
alignment: PositionalTabAlignment.RIGHT,
relativeTo: PositionalTabRelativeTo.MARGIN,
leader: PositionalTabLeader.DOT,
}),
"3",
]}),
],
})
多栏布局
// 等宽栏
sections: [{
properties: {
column: {
count: 2, // 栏数
space: 720, // 栏间距,DXA 单位(720 = 0.5 英寸)
equalWidth: true,
separate: true, // 栏间竖线
},
},
children: [/* 内容在各栏间自然流动 */]
}]
// 自定义宽度栏(equalWidth 必须为 false)
sections: [{
properties: {
column: {
equalWidth: false,
children: [
new Column({ width: 5400, space: 720 }),
new Column({ width: 3240 }),
],
},
},
children: [/* content */]
}]
使用 type: SectionType.NEXT_COLUMN 创建新节来强制分栏。
目录
// 关键:标题必须仅使用 HeadingLevel - 不使用自定义样式
new TableOfContents("Table of Contents", { hyperlink: true, headingStyleRange: "1-3" })
页眉/页脚
sections: [{
properties: {
page: { margin: { top: 1440, right: 1440, bottom: 1440, left: 1440 } } // 1440 = 1 英寸
},
headers: {
default: new Header({ children: [new Paragraph({ children: [new TextRun("Header")] })] })
},
footers: {
default: new Footer({ children: [new Paragraph({
children: [new TextRun("Page "), new TextRun({ children: [PageNumber.CURRENT] })]
})] })
},
children: [/* content */]
}]
docx-js 关键规则
- 明确设置页面大小 - docx-js 默认为 A4;美国文档使用 US Letter(12240 x 15840 DXA)
- 横向:传入纵向尺寸 - docx-js 会在内部交换宽度/高度;将短边作为
width,长边作为height,并设置orientation: PageOrientation.LANDSCAPE - 绝不要使用
\n- 使用单独的 Paragraph 元素 - 绝不要使用 Unicode 项目符号 - 使用带编号配置的
LevelFormat.BULLET - PageBreak 必须在 Paragraph 中 - 单独使用会创建无效的 XML
- ImageRun 需要
type- 始终指定 png/jpg 等 - 始终用 DXA 设置表格
width- 绝不要使用WidthType.PERCENTAGE(在 Google Docs 中会出问题) - 表格需要双重宽度设置 -
columnWidths数组和单元格width,两者必须匹配 - 表格宽度 = columnWidths 之和 - 对于 DXA,确保它们精确相加
- 始终添加单元格边距 - 使用
margins: { top: 80, bottom: 80, left: 120, right: 120 }获得可读的内边距 - 使用
ShadingType.CLEAR- 绝不要用 SOLID 作为表格底纹 - 绝不要用表格作为分隔线 - 单元格有最小高度,会渲染为空白框(包括在页眉/页脚中);改用在 Paragraph 上的
border: { bottom: { style: BorderStyle.SINGLE, size: 6, color: "2E75B6", space: 1 } }。对于两栏页脚,使用制表位(参见制表位部分),而不是表格 - 目录仅需要 HeadingLevel - 标题段落上不要使用自定义样式
- 覆盖内置样式 - 使用精确的 ID:"Heading1"、"Heading2" 等
- 包含
outlineLevel- 目录所必需(H1 为 0,H2 为 1,以此类推)
编辑现有文档
按顺序执行所有 3 个步骤。
步骤 1:解压
python scripts/office/unpack.py document.docx unpacked/
提取 XML、格式化输出、合并相邻的 run,并将智能引号转换为 XML 实体(“ 等),以便它们在编辑后仍然保留。使用 --merge-runs false 跳过 run 合并。
步骤 2:编辑 XML
编辑 unpacked/word/ 中的文件。参见下方的 XML 参考了解模式。
使用 "Claude" 作为作者 用于修订追踪和批注,除非用户明确要求使用不同的名称。
直接使用 Edit 工具进行字符串替换。不要编写 Python 脚本。 脚本会引入不必要的复杂性。Edit 工具会准确显示正在替换的内容。
关键:对新内容使用智能引号。 当添加带有撇号或引号的文本时,使用 XML 实体来生成智能引号:
<!-- 使用这些实体实现专业排版 -->
<w:t>Here’s a quote: “Hello”</w:t>
| 实体 | 字符 |
|---|---|
‘ |
'(左单引号) |
’ |
'(右单引号 / 撇号) |
“ |
"(左双引号) |
” |
"(右双引号) |
添加批注: 使用 comment.py 处理跨多个 XML 文件的样板代码(文本必须是预转义的 XML):
python scripts/comment.py unpacked/ 0 "Comment text with & and ’"
python scripts/comment.py unpacked/ 1 "Reply text" --parent 0 # 回复批注 0
python scripts/comment.py unpacked/ 0 "Text" --author "Custom Author" # 自定义作者名
然后在 document.xml 中添加标记(参见 XML 参考中的批注部分)。
步骤 3:打包
python scripts/office/pack.py unpacked/ output.docx --original document.docx
验证并自动修复、压缩 XML,并创建 DOCX。使用 --validate false 跳过验证。
自动修复将处理:
durableId>= 0x7FFFFFFF(重新生成有效 ID)- 带有空白字符的
<w:t>上缺少xml:space="preserve"
自动修复无法处理:
- 格式错误的 XML、无效的元素嵌套、缺少的关系、架构违规
常见陷阱
- 替换整个
<w:r>元素:添加修订追踪时,将整个<w:r>...</w:r>块替换为作为兄弟元素的<w:del>...<w:ins>...。不要在 run 内部注入修订追踪标签。 - 保留
<w:rPr>格式:将原始 run 的<w:rPr>块复制到修订追踪 run 中,以保持粗体、字体大小等。
XML 参考
架构合规
<w:pPr>中的元素顺序:<w:pStyle>、<w:numPr>、<w:spacing>、<w:ind>、<w:jc>、<w:rPr>最后- 空白字符:对带有前导/尾随空格的
<w:t>添加xml:space="preserve" - RSID:必须是 8 位十六进制(例如
00AB1234)
修订追踪
插入:
<w:ins w:id="1" w:author="Claude" w:date="2025-01-01T00:00:00Z">
<w:r><w:t>inserted text</w:t></w:r>
</w:ins>
删除:
<w:del w:id="2" w:author="Claude" w:date="2025-01-01T00:00:00Z">
<w:r><w:delText>deleted text</w:delText></w:r>
</w:del>
在 <w:del> 内部:使用 <w:delText> 代替 <w:t>,使用 <w:delInstrText> 代替 <w:instrText>。
最小化编辑 - 只标记变化的部分:
<!-- 将 "30 days" 改为 "60 days" -->
<w:r><w:t>The term is </w:t></w:r>
<w:del w:id="1" w:author="Claude" w:date="...">
<w:r><w:delText>30</w:delText></w:r>
</w:del>
<w:ins w:id="2" w:author="Claude" w:date="...">
<w:r><w:t>60</w:t></w:r>
</w:ins>
<w:r><w:t> days.</w:t></w:r>
删除整个段落/列表项 - 当删除段落中的所有内容时,也要将段落标记标记为已删除,以便它与下一个段落合并。在 <w:pPr><w:rPr> 内添加 <w:del/>:
<w:p>
<w:pPr>
<w:numPr>...</w:numPr> <!-- 列表编号(如果存在) -->
<w:rPr>
<w:del w:id="1" w:author="Claude" w:date="2025-01-01T00:00:00Z"/>
</w:rPr>
</w:pPr>
<w:del w:id="2" w:author="Claude" w:date="2025-01-01T00:00:00Z">
<w:r><w:delText>Entire paragraph content being deleted...</w:delText></w:r>
</w:del>
</w:p>
如果没有在 <w:pPr><w:rPr> 中添加 <w:del/>,接受更改后会留下一个空段落/列表项。
拒绝其他作者的插入 - 在其插入内部嵌套删除:
<w:ins w:author="Jane" w:id="5">
<w:del w:author="Claude" w:id="10">
<w:r><w:delText>their inserted text</w:delText></w:r>
</w:del>
</w:ins>
恢复其他作者的删除 - 在其后添加插入(不要修改他们的删除):
<w:del w:author="Jane" w:id="5">
<w:r><w:delText>deleted text</w:delText></w:r>
</w:del>
<w:ins w:author="Claude" w:id="10">
<w:r><w:t>deleted text</w:t></w:r>
</w:ins>
批注
运行 comment.py 后(参见步骤 2),在 document.xml 中添加标记。对于回复,使用 --parent 标志并将标记嵌套在父标记内部。
关键:<w:commentRangeStart> 和 <w:commentRangeEnd> 是 <w:r> 的兄弟元素,绝不在 <w:r> 内部。
<!-- 批注标记是 w:p 的直接子元素,绝不在 w:r 内部 -->
<w:commentRangeStart w:id="0"/>
<w:del w:id="1" w:author="Claude" w:date="2025-01-01T00:00:00Z">
<w:r><w:delText>deleted</w:delText></w:r>
</w:del>
<w:r><w:t> more text</w:t></w:r>
<w:commentRangeEnd w:id="0"/>
<w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="0"/></w:r>
<!-- 批注 0 内嵌套回复 1 -->
<w:commentRangeStart w:id="0"/>
<w:commentRangeStart w:id="1"/>
<w:r><w:t>text</w:t></w:r>
<w:commentRangeEnd w:id="1"/>
<w:commentRangeEnd w:id="0"/>
<w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="0"/></w:r>
<w:r><w:rPr><w:rStyle w:val="CommentReference"/></w:rPr><w:commentReference w:id="1"/></w:r>
图片
- 将图片文件添加到
word/media/ - 在
word/_rels/document.xml.rels中添加关系:
<Relationship Id="rId5" Type=".../image" Target="media/image1.png"/>
- 在
[Content_Types].xml中添加内容类型:
<Default Extension="png" ContentType="image/png"/>
- 在 document.xml 中引用:
<w:drawing>
<wp:inline>
<wp:extent cx="914400" cy="914400"/> <!-- EMUs: 914400 = 1 英寸 -->
<a:graphic>
<a:graphicData uri=".../picture">
<pic:pic>
<pic:blipFill><a:blip r:embed="rId5"/></pic:blipFill>
</pic:pic>
</a:graphicData>
</a:graphic>
</wp:inline>
</w:drawing>
依赖项
- pandoc:文本提取
- docx:
npm install -g docx(新文档) - LibreOffice:PDF 转换(通过
scripts/office/soffice.py为沙盒环境自动配置) - Poppler:
pdftoppm用于图片