Create word doc

From insmtx/Lework · View interactive version

Create word doc is an AI agent skill from the GitHub repository insmtx/Lework. 创建、编辑 .docx 文件(报告、合同、提案)。当需要生成 Word 文档、从头创建文档、编辑现有文档、使用模板填充、提取文本时使用。处理报告、备忘录、合同、邀请函、证书等交付物。

How to use this skill

Skill file

Create Word Doc

将主题、草稿、大纲或现有文档转换为专业的 Word 文档。分为两个相连的阶段:首先设计文档结构和内容,然后生成/编辑 .docx 并检查布局。

需要更深入的写作和审阅指导时,请阅读 references/word-document-workflow.md。

核心原则

工具选择矩阵

| 任务 | 工具/库 | 语言 | 适用场景 |

|------|---------|------|----------|

| 创建 DOCX | python-docx | Python | 报告、合同、提案 |

| 创建 DOCX | docx | Node.js | 服务端文档生成、TypeScript 项目 |

| DOCX 转 HTML | mammoth.js | Node.js | Web 展示、内容提取 |

| 解析 DOCX | python-docx | Python | 提取文本、表格、元数据 |

| 模板填充 | docxtpl | Python | 邮件合并、基于模板的生成 |

| 审阅流程 | Word 比较/评论/高亮 | 任意 | 人工审阅,无需 OOXML 手术 |

| 修订记录 | OOXML 检查 | 任意 | 真正的修订痕迹或解析修订记录 |

工具选用建议

已知限制

工作流程

1. 明确文档概要

确定最少的必要信息:

用户未指定时,根据任务推断保守默认值并简要说明。

2. 设计大纲

根据文档用途选择结构:

长文档每个顶级部分负责一个问题。添加子目录仅在改善导航时使用。

3. 充实内容

逐节从大纲扩展到草稿:

避免填充模式:重复介绍、通用好处、未经支持的夸张表述、冗余的结尾段落。

4. 创建/编辑 DOCX

Python 示例 (python-docx)
from docx import Document
from docx.shared import Inches, Pt, Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH

doc = Document()

# 标题
title = doc.add_heading('文档标题', 0)
title.alignment = WD_ALIGN_PARAGRAPH.CENTER

# 带格式的段落
para = doc.add_paragraph()
run = para.add_run('这是粗体 ')
run.bold = True
run = para.add_run('和斜体文本。')
run.italic = True

# 表格
table = doc.add_table(rows=3, cols=3)
table.style = 'Table Grid'
for i, row in enumerate(table.rows):
    for j, cell in enumerate(row.cells):
        cell.text = f'第 {i+1} 行,第 {j+1} 列'

# 图片
doc.add_picture('image.png', width=Inches(4))

# 保存
doc.save('output.docx')
模板填充 (docxtpl)
from docxtpl import DocxTemplate

doc = DocxTemplate('template.docx')
context = {
    'company_name': '某某公司',
    'date': '2025-01-15',
    'items': [
        {'name': '产品 A', 'price': 100},
        {'name': '产品 B', 'price': 200},
    ]
}
doc.render(context)
doc.save('filled_template.docx')
Node.js 示例 (docx)
import { Document, Packer, Paragraph, TextRun, Table, TableRow, TableCell } from 'docx';
import * as fs from 'fs';

const doc = new Document({
  sections: [{
    properties: {},
    children: [
      new Paragraph({
        children: [
          new TextRun({ text: '粗体文本', bold: true }),
          new TextRun({ text: ' 和普通文本。' }),
        ],
      }),
      new Table({
        rows: [
          new TableRow({
            children: [
              new TableCell({ children: [new Paragraph('单元格 1')] }),
              new TableCell({ children: [new Paragraph('单元格 2')] }),
            ],
          }),
        ],
      }),
    ],
  }],
});

Packer.toBuffer(doc).then((buffer) => {
  fs.writeFileSync('output.docx', buffer);
});

5. 验证交付

6. 编辑现有文档

样式参考

| 元素 | Python 方法 | Node.js 类 |

|------|-------------|------------|

| 标题 1 | add_heading(text, 1) | HeadingLevel.HEADING_1 |

| 粗体 | run.bold = True | TextRun({ bold: true }) |

| 斜体 | run.italic = True | TextRun({ italics: true }) |

| 字体大小 | run.font.size = Pt(12) | TextRun({ size: 24 }) (半磅) |

| 对齐 | WD_ALIGN_PARAGRAPH.CENTER | AlignmentType.CENTER |

| 分页符 | doc.add_page_break() | new PageBreak() |

质量检查清单

注意事项

应该做

避免

禁止

交付约定

导航

参考资料 (references/)

脚本工具 (scripts/)

模板资源 (assets/)

相关技能

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.