docx

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

DOCX 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 template
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 template

Core Workflows

核心工作流

1. Document Generation (python-docx)

1. 文档生成(python-docx)

  1. Create Document object
  2. Add heading with level
  3. Add paragraphs with text
  4. Apply styles (built-in or custom)
  5. Add tables if needed
  6. Insert images
  7. Save document
  1. 创建Document对象
  2. 添加指定级别的标题
  3. 添加带文本的段落
  4. 应用样式(内置或自定义)
  5. 按需添加表格
  6. 插入图片
  7. 保存文档

2. Template Processing

2. 模板处理

  1. Load template document
  2. Find placeholders ({{variable}})
  3. Replace with actual values
  4. Handle conditional sections
  5. Process repeating sections
  6. Save as new document
  1. 加载模板文档
  2. 查找占位符({{variable}})
  3. 替换为实际值
  4. 处理条件性章节
  5. 处理重复章节
  6. 另存为新文档

3. Batch Document Generation

3. 批量文档生成

  1. Load template once
  2. Iterate over data records
  3. Clone template for each
  4. Fill placeholders
  5. Generate unique filenames
  6. Save each document
  1. 仅加载一次模板
  2. 遍历数据记录
  3. 为每条记录克隆模板
  4. 填充占位符
  5. 生成唯一文件名
  6. 保存每个文档

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-PatternProblemCorrect Approach
Direct formattingHard to maintainUse styles
Modifying templateCorrupts originalSave to new file
No error handlingFails on bad inputValidate data first
Hardcoded pathsNot portableUse relative paths
Ignoring encodingCharacter issuesUse UTF-8 strings
反模式问题正确做法
直接格式化难以维护使用样式
修改原始模板损坏原始文件另存为新文件
无错误处理遇到无效输入时失败先验证数据
硬编码路径不具备可移植性使用相对路径
忽略编码出现字符问题使用UTF-8字符串