Use this skill whenever the user wants to create, read, edit, or manipulate Word documents (.docx files). Triggers include: any mention of "Word doc", "word document", ".docx", or requests to produce professional documents with formatting like tables of contents, headings, page numbers, or letterheads. Also use when extracting or reorganizing content from .docx files, inserting or replacing images in documents, performing find-and-replace in Word files, working with tracked changes or comments, or converting content into a polished Word document. If the user asks for a "report", "memo", "letter", "template", or similar deliverable as a Word or .docx file, use this skill. Do NOT use for PDFs, spreadsheets, Google Docs, or general coding tasks unrelated to document generation. Use when 用户提到 Word文档、docx、创建文档、编辑文档、报告、 备忘录、公文、合同、信函模板。
创建、编辑和处理 Word 文档(.docx),支持新建、修改 XML、格式校验全流程。
docx 是一个流程型技能(Procedural Skill),提供 Word 文档的完整处理能力。支持通过 docx-js(Node.js)创建新文档,通过解包 XML 编辑现有文档,以及格式验证和 PDF 转换。
在执行任何 Python 脚本之前,先检测 Python 是否可用:
python3 --version 2>/dev/null || python --version 2>/dev/null
如果命令失败(Python 不可用),必须停止并告知用户安装 Python 3:
brew install python3 或从 https://www.python.org/downloads/ 下载winget install Python.Python.3 或从 python.org 下载(安装时勾选 "Add Python to PATH")sudo apt install python3 python3-pipsudo dnf install python3 python3-pip如需更详细的环境配置帮助,加载 environment-setup 技能。
本技能的 Python 脚本依赖以下包(按需检测,仅在实际调用相关脚本时检查):
lxml — XML schema 验证(validate.py)defusedxml — 安全 XML 解析(unpack.py)检测方法:
python3 -c "import lxml; import defusedxml" 2>/dev/null || echo "MISSING"
缺失时告知用户安装:pip install lxml defusedxml
When you create or modify a .docx file, you MUST tell the user the absolute path of the output file in your response. Example: "文件已保存到:/path/to/output.docx"
A .docx file is a ZIP archive containing XML files.
| Task | Approach |
|---|---|
| Read/analyze content | pandoc or unpack for raw XML |
| Create new document | Use docx-js - see Creating New Documents below |
| Edit existing document | Unpack → edit XML → repack - see Editing Existing Documents below |
Legacy .doc files must be converted before editing:
python scripts/office/soffice.py --headless --convert-to docx document.doc
# Text extraction with tracked changes
pandoc --track-changes=all document.docx -o output.md
# Raw XML access
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
To produce a clean document with all tracked changes accepted (requires LibreOffice):
python scripts/accept_changes.py input.docx output.docx
Generate .docx files with JavaScript, then validate. Install: npm install -g docx
const { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell, ImageRun,
Header, Footer, AlignmentType, PageOrientation, LevelFormat, ExternalHyperlink,
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));
After creating the file, validate it. If validation fails, unpack, fix the XML, and repack.
python scripts/office/validate.py doc.docx
// CRITICAL: docx-js defaults to A4, not US Letter
// Always set page size explicitly for consistent results