docx
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseDOCX Skill
DOCX 技能
Purpose
用途
Provides expertise in programmatic Word document creation and manipulation. Handles document generation, template filling, style management, and batch document processing using python-docx and JavaScript libraries.
提供程序化创建和处理Word文档的专业能力。可使用python-docx和JavaScript库处理文档生成、模板填充、样式管理以及批量文档处理。
When to Use
适用场景
- Generating Word documents programmatically
- Filling document templates with data
- Modifying existing .docx files
- Extracting content from Word documents
- Applying styles and formatting
- Creating mail merge workflows
- Converting data to formatted documents
- 程序化生成Word文档
- 为文档模板填充数据
- 修改现有.docx文件
- 从Word文档中提取内容
- 应用样式和格式设置
- 创建邮件合并工作流
- 将数据转换为格式化文档
Quick Start
快速入门
Invoke this skill when:
- Generating Word documents programmatically
- Filling document templates with data
- Modifying existing .docx files
- Extracting content from Word documents
- Automating document workflows
Do NOT invoke when:
- Writing document content (use document-writer)
- Creating PDFs (use pdf-skill)
- Creating spreadsheets (use xlsx-skill)
- Creating presentations (use pptx-skill)
调用本技能的场景:
- 程序化生成Word文档
- 为文档模板填充数据
- 修改现有.docx文件
- 从Word文档中提取内容
- 自动化文档工作流
请勿调用本技能的场景:
- 编写文档内容(请使用文档编写技能)
- 创建PDF文件(请使用PDF技能)
- 创建电子表格(请使用XLSX技能)
- 创建演示文稿(请使用PPTX技能)
Decision Framework
决策框架
Library Selection:
├── Python backend → python-docx
├── Node.js backend → docx (npm)
├── Browser-based → docx.js
├── Complex templates → docxtemplater
└── Simple text extraction → mammoth
Task Type:
├── Generate from scratch → Build document programmatically
├── Fill template → Use placeholder replacement
├── Modify existing → Load, edit, save
└── Batch processing → Loop with templateLibrary Selection:
├── Python backend → python-docx
├── Node.js backend → docx (npm)
├── Browser-based → docx.js
├── Complex templates → docxtemplater
└── Simple text extraction → mammoth
Task Type:
├── Generate from scratch → Build document programmatically
├── Fill template → Use placeholder replacement
├── Modify existing → Load, edit, save
└── Batch processing → Loop with templateCore Workflows
核心工作流
1. Document Generation (python-docx)
1. 文档生成(python-docx)
- Create Document object
- Add heading with level
- Add paragraphs with text
- Apply styles (built-in or custom)
- Add tables if needed
- Insert images
- Save document
- 创建Document对象
- 添加指定级别的标题
- 添加带文本的段落
- 应用样式(内置或自定义)
- 按需添加表格
- 插入图片
- 保存文档
2. Template Processing
2. 模板处理
- Load template document
- Find placeholders ({{variable}})
- Replace with actual values
- Handle conditional sections
- Process repeating sections
- Save as new document
- 加载模板文档
- 查找占位符({{variable}})
- 替换为实际值
- 处理条件性章节
- 处理重复章节
- 另存为新文档
3. Batch Document Generation
3. 批量文档生成
- Load template once
- Iterate over data records
- Clone template for each
- Fill placeholders
- Generate unique filenames
- Save each document
- 仅加载一次模板
- 遍历数据记录
- 为每条记录克隆模板
- 填充占位符
- 生成唯一文件名
- 保存每个文档
Best Practices
最佳实践
- Use paragraph styles, not direct formatting
- Create templates with placeholders for reuse
- Handle missing placeholder values gracefully
- Preserve original template, save to new file
- Test with complex content (tables, images)
- Validate output opens correctly
- 使用段落样式,而非直接格式化
- 创建带占位符的模板以便复用
- 优雅处理缺失的占位符值
- 保留原始模板,另存为新文件
- 使用复杂内容(表格、图片)进行测试
- 验证输出文件可正常打开
Anti-Patterns
反模式
| Anti-Pattern | Problem | Correct Approach |
|---|---|---|
| Direct formatting | Hard to maintain | Use styles |
| Modifying template | Corrupts original | Save to new file |
| No error handling | Fails on bad input | Validate data first |
| Hardcoded paths | Not portable | Use relative paths |
| Ignoring encoding | Character issues | Use UTF-8 strings |
| 反模式 | 问题 | 正确做法 |
|---|---|---|
| 直接格式化 | 难以维护 | 使用样式 |
| 修改原始模板 | 损坏原始文件 | 另存为新文件 |
| 无错误处理 | 遇到无效输入时失败 | 先验证数据 |
| 硬编码路径 | 不具备可移植性 | 使用相对路径 |
| 忽略编码 | 出现字符问题 | 使用UTF-8字符串 |