Docx
v1.0.0Use this 技能 whenever the user wants to 创建, read, edit, or manipulate Word documents (.docx files). Triggers include: any mention of 'Word doc', 'word document', '.docx', or 请求s to produce professional documents with 格式化ting like tables of contents, headings, page numbers, or letterheads. Also use when 提取ing or reorganizing content from .docx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with 追踪ed changes or comments, or converting content into a polished Word document. If the user asks for a '报告', 'memo', 'letter', 'template', or similar deliverable as a Word or .docx file, use this 技能. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation.
运行时依赖
版本
pandoc --追踪-changes=all document.docx -o 输出.md
安装命令
点击复制技能文档
DOCX creation, editing, and analysis Overview
A .docx file is a ZIP 归档 contAIning XML files.
Quick Reference Task 应用roach Read/analyze content pandoc or unpack for raw XML 创建 new document Use docx-js - see Creating New Documents below Edit existing document Unpack → edit XML → repack - see Editing Existing Documents below Converting .doc to .docx
Legacy .doc files must be converted before editing:
python scripts/office/soffice.py --headless --convert-to docx document.doc
Reading Content # Text 提取ion with 追踪ed changes pandoc --追踪-changes=all document.docx -o 输出.md
# Raw XML 访问 python scripts/office/unpack.py document.docx unpacked/
Converting to Images python scripts/office/soffice.py --headless --convert-to pdf document.docx pdftoppm -jpeg -r 150 document.pdf page
Accepting 追踪ed Changes
To produce a 清理 document with all 追踪ed changes accepted (requires LibreOffice):
python scripts/accept_changes.py 输入.docx 输出.docx
Creating New Documents
生成 .docx files with JavaScript, then 验证. 安装: npm 安装 -g docx
设置up const { Document, Packer, Paragraph, Text运行, Table, TableRow, TableCell, Image运行, Header, Footer, AlignmentType, PageOrientation, Level格式化, ExternalHyperlink, InternalHyperlink, Bookmark, FootnoteReference运行, PositionalTab, PositionalTabAlignment, PositionalTabRelativeTo, PositionalTabLeader, Tab停止Type, Tab停止Position, 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.writeFile同步("doc.docx", buffer));
验证
After creating the file, 验证 it. If 验证 fAIls, unpack, fix the XML, and repack.
python scripts/office/验证.py doc.docx
Page Size // CRITICAL: docx-js defaults to A4, not US Letter // Always 设置 page size explicitly for consistent 结果s sections: [{ properties: { page: { size: { width: 12240, // 8.5 inches in DXA height: 15840 // 11 inches in DXA }, margin: { top: 1440, right: 1440, 机器人tom: 1440, left: 1440 } // 1 inch margins } }, children: [/ content /] }]
Common page sizes (DXA units, 1440 DXA = 1 inch):
Paper Width Height Content Width (1" margins) US Letter 12,240 15,840 9,360 A4 (default) 11,906 16,838 9,026
Landscape orientation: docx-js swaps width/height internally, so pass portrAIt dimensions and let it handle the swap:
size: { width: 12240, // Pass SHORT edge as width height: 15840, // Pass LONG edge as height orientation: PageOrientation.LANDSCAPE // docx-js swaps them in the XML }, // Content width = 15840 - left margin - right margin (uses the long edge)
Styles (Override Built-in Headings)
Use Arial as the default font (universally supported). Keep titles black for readability.
const doc = new Document({ styles: { default: { document: { 运行: { font: "Arial", size: 24 } } }, // 12pt default paragraphStyles: [ // 导入ANT: Use exact IDs to override built-in styles { id: "Heading1", name: "Heading 1", basedOn: "Normal", next: "Normal", quick格式化: true, 运行: { size: 32, bold: true, font: "Arial" }, paragraph: { spacing: { before: 240, after: 240 }, outlineLevel: 0 } }, // outlineLevel required for TOC { id: "Heading2", name: "Heading 2", basedOn: "Normal", next: "Normal", quick格式化: true, 运行: { size: 28, bold: true, font: "Arial" }, paragraph: { spacing: { before: 180, after: 180 }, outlineLevel: 1 } }, ] }, sections: [{ children: [ new Paragraph({ heading: HeadingLevel.HEADING_1, children: [new Text运行("Title")] }), ] }] });
列出s (NEVER use unicode bullets) // ❌ WRONG - never manually insert bullet characters new Paragraph({ children: [new Text运行("• Item")] }) // BAD new Paragraph({ children: [new Text运行("\u2022 Item")] }) // BAD
// ✅ CORRECT - use numbering config with Level格式化.BULLET const doc = new Document({ numbering: { config: [ { reference: "bullets", levels: [{ level: 0, 格式化: Level格式化.BULLET, text: "•", alignment: AlignmentType.LEFT, style: { paragraph: { indent: { left: 720, hanging: 360 } } } }] }, { reference: "numbers", levels: [{ level: 0, 格式化: Level格式化.DECIMAL, text: "%1.", alignment: AlignmentType.LEFT, style: { paragraph: { indent: { left: 720, hanging: 360 } } } }] }, ] }, sections: [{ children: [ new Paragraph({ numbering: { reference: "bullets", level: 0 }, children: [new Text运行("Bullet item")] }), new Paragraph({ numbering: { reference: "numbers", level: 0 }, children: [new Text运行("Numbered item")] }), ] }] });
// ⚠️ Each reference 创建s INDEPENDENT numbering // Same reference = continues (1,2,3 then 4,5,6) // Different refere