| name | wps-docx-writer |
| description | Word文档生成。用python-docx生成各类Word文档,
支持标题、正文、表格、图片、页眉页脚等元素。
用于帮助用户生成Word文档。当用户提到Word、docx、文档时触发。
Word document generator using python-docx.
|
| license | MIT |
| user-invocable | true |
| argument-hint | [文档类型] [内容描述] |
| allowed-tools | Read, Grep, Glob, Bash, Write, Edit |
| metadata | {"author":"BWKYD","title":"Word文档生成","description_zh":"用python-docx生成Word文档,支持标题、正文、表格、图片等","tags":["Word","docx","python-docx","文档生成","WPS"],"version":"1.0.3","license":"MIT"} |
Word 文档生成器
使用 python-docx 生成各类规范的中文 .docx 文档,兼容 WPS Office 和 Microsoft Word。
When to Use
- 用户需要生成 Word/docx 文档
- 用户需要创建合同、报告、简历、论文、方案等
- 用户需要批量生成文档或填充模板
- 用户说"帮我写一份XX"且不是公文场景
When NOT to Use
- 党政机关公文 → 使用
wps-gongwen
- WPS宏脚本开发 → 使用
wps-jsa-macro
- 纯文本/Markdown文档 → 直接编写
支持的文档类型
| 类型 | 典型场景 | 模板特征 |
|---|
| 合同/协议 | 劳动合同、租赁合同、保密协议、合作协议 | 甲乙方信息、条款编号、签章区 |
| 报告 | 工作报告、调研报告、分析报告、可行性报告 | 封面、目录、章节标题、图表 |
| 简历 | 中式求职简历 | 照片位、个人信息表、工作经历 |
| 方案/计划 | 项目方案、实施计划、营销方案 | 背景、目标、步骤、预算 |
| 论文 | 毕业论文、学术论文 | 封面、摘要、关键词、参考文献 |
| 信函 | 商业函件、邀请函、感谢信 | 抬头、正文、落款 |
| 表格文档 | 考勤表、登记表、审批表 | 多列表格、合并单元格 |
工作流程
Step 1: 需求确认
确认以下信息:
- 文档类型:合同/报告/简历/方案/论文/信函/其他
- 主要内容:核心内容描述
- 特殊要求:页面方向、字体偏好、是否需要封面/目录等
Step 2: 内容撰写
根据文档类型撰写内容,遵循中文商务/学术写作规范。
Step 3: 生成 .docx 文件
先确保依赖已安装:
pip install python-docx 2>/dev/null || pip3 install python-docx 2>/dev/null
使用以下核心代码框架:
from docx import Document
from docx.shared import Pt, Mm, Cm, Inches, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.enum.table import WD_TABLE_ALIGNMENT
from docx.enum.section import WD_ORIENT
from docx.oxml.ns import qn, nsdecls
from docx.oxml import parse_xml
import os
class DocxWriter:
"""通用中文docx文档生成器"""
STYLES = {
'title': {'font': '方正小标宋简体', 'size': 22, 'bold': True, 'align': 'CENTER'},
'heading1': {'font': '黑体', 'size': 18, 'bold': True, 'align': 'LEFT'},
'heading2': {'font': '黑体', 'size': 16, 'bold': True, 'align': 'LEFT'},
'heading3': {'font': '黑体', 'size': 14, 'bold': , : },
: {: , : , : , : },
:{: , : , : , : },
: {: , : ,: , : },
: {: , : , : , : },
}
():
.doc = Document()
section = .doc.sections[]
orientation == :
section.orientation = WD_ORIENT.LANDSCAPE
section.page_width = Mm()
section.page_height = Mm()
:
section.page_width = Mm()
section.page_height = Mm()
m = margins {: , : , : , : }
section.top_margin = Mm(m[])
section.bottom_margin = Mm(m[])
section.left_margin = Mm(m[])
section.right_margin = Mm(m[])
():
s = .STYLES.get(style, .STYLES[])
p = .doc.add_paragraph()
p.alignment = (WD_ALIGN_PARAGRAPH, s[])
p.paragraph_format.space_before = Pt(space_before)
p.paragraph_format.space_after = Pt(space_after)
line_spacing:
p.paragraph_format.line_spacing = Pt(line_spacing)
first_indent :
p.paragraph_format.first_line_indent = Pt(first_indent)
style == style == :
p.paragraph_format.first_line_indent = Pt(s[] * )
run = p.add_run(text)
run.font.size = Pt(s[])
run.font.name = s[]
run._element.rPr.rFonts.(qn(), s[])
run.bold = s[]
color:
run.font.color.rgb = RGBColor(*color)
p
():
table = .doc.add_table(rows= + (rows), cols=(headers), style=style)
table.alignment = WD_TABLE_ALIGNMENT.CENTER
i, header (headers):
cell = table.rows[].cells[i]
cell.text = header
paragraph cell.paragraphs:
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
run paragraph.runs:
run.bold =
run.font.size = Pt()
run.font.name =
run._element.rPr.rFonts.(qn(), )
r_idx, row_data (rows):
c_idx, cell_text (row_data):
cell = table.rows[r_idx + ].cells[c_idx]
cell.text = (cell_text)
paragraph cell.paragraphs:
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
run paragraph.runs:
run.font.size = Pt()
run.font.name =
run._element.rPr.rFonts.(qn(), )
col_widths:
i, width (col_widths):
row table.rows:
row.cells[i].width = Mm(width)
table
():
.doc.add_page_break()
():
_ ():
.doc.add_paragraph()
.add_text(title, , space_after=)
subtitle:
.add_text(subtitle, , space_after=)
org:
.add_text(org, , space_after=)
date_str:
.add_text(date_str, )
.add_page_break()
():
p = .doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = p.add_run()
run.font.size = Pt()
run.font.name =
run._element.rPr.rFonts.(qn(), )
run.bold =
p2 = .doc.add_paragraph()
p2.alignment = WD_ALIGN_PARAGRAPH.CENTER
run2 = p2.add_run()
run2.font.size = Pt()
run2.font.name =
run2._element.rPr.rFonts.(qn(), )
run2.font.color.rgb = RGBColor(, , )
.add_page_break()
():
.doc.save(filename)
os.path.abspath(filename)
Step 4: 各文档类型具体指引
合同/协议
结构:
合同编号
合同标题
甲乙方信息(名称、地址、法定代表人、联系方式)
鉴于条款(背景)
正文条款(逐条编号)
违约责任
争议解决(仲裁/诉讼、管辖地)
其他约定
签署栏(甲方签章、乙方签章、日期)
注意:
- 条款编号使用"第X条"格式
- 金额须同时标注大小写
- 日期格式:XXXX年XX月XX日
- 签署栏左右分列(甲方左、乙方右)
报告
结构:
封面(标题、编制单位、日期)
目录
正文章节(一、二、三…)
结论/建议
附件/附录
注意:
- 章节标题层级清晰
- 数据用表格或列表呈现
- 图表需编号和标题
简历(中式)
结构:
标题栏(姓名、照片位)
基本信息表(性别、出生年月、民族、政治面貌、籍贯、学历、联系方式)
求职意向
教育背景
工作经历
项目经验
技能特长
自我评价
注意:
- 照片位右上角 35mm×45mm
- 基本信息用表格排列
- 控制在1-2页
Step 5: 交付
- 生成 .docx 文件
- 告知文件路径和大小
- 提示用WPS打开检查
- 按需调整
示例
/wps-docx-writer 合同 生成一份劳动合同,甲方为XX科技有限公司,合同期限3年
/wps-docx-writer 报告 编写一份2026年Q1项目进度报告,需要封面和目录
/wps-docx-writer 简历 生成一份软件工程师求职简历模板
输出:在当前目录生成对应的 .docx 文件,用WPS打开即可编辑。
格式参考
中文排版通用规范见 ../shared/chinese-typesetting.md。
python-docx 高级用法见 reference/python-docx-advanced.md。