| name | document-editor |
| description | Advanced document editing for Word and Excel with formal formatting, including Chinese government document standards, table styling, and cell formatting. |
| metadata | {"openclaw":{"emoji":"📝","requires":{"bins":["python3"]}}} |
Document Editor Skill 📝
高级文档编辑工具,支持Word和Excel深度格式化,完美适配机关公文格式规范国家标准。
📋 公文格式规范(国家标准)
页面设置
| 项目 | 参数 |
|---|
| 纸张 | A4(210mm × 297mm) |
| 上边距 | 3.7 cm |
| 下边距 | 3.5 cm |
| 左边距 | 2.8 cm |
| 右边距 | 2.6 cm |
| 文档网格 | 每页22行、每行28字 |
| 行距 | 固定值 28 磅 |
字体配置
| 元素 | 字体 | 字号 | 加粗 | 行距 | 与前段空行 |
|---|
| 总标题 | 方正小标宋简体 | 二号 | ❌ | 32磅 | — |
| 标题下单位/署名 | 楷体_GB2312 | 三号 | ❌ | 28磅 | ❌ |
| 主送机关/正文 | 仿宋_GB2312 | 三号 | ❌ | 28磅 | ✅ |
| 一级标题(一、) | 黑体 | 三号 | ❌ | 28磅 | ✅ |
| 二级标题((一)) | 楷体_GB2312 | 三号 | ❌ | 28磅 | ✅ |
| 三级标题(一是) | 仿宋_GB2312 | 三号 | ✅ | 28磅 | ✅ |
| 落款 | 仿宋_GB2312 | 三号 | ❌ | 28磅 | ✅ |
| 页码 | 宋体 | 四号 | ❌ | 28磅 | — |
三级标题格式细节(重要):
- 格式如:"一是严格值班纪律。" "二是做好安全检查。" "三是加强检查。"
- 只加粗标题本身(到句号为止),解释说明不加粗
- 示例:一是严格值班纪律。 值班人员不得擅自离岗,遇紧急情况应及时报告带班领导。
- 每个"一是/二是/三是"应作为独立段落,避免加粗标题连在一起
- 英文数字使用 Times New Roman 字体
微信发送注意:
- 表格请用 Markdown 代码块(```)格式发送,微信支持左右滑动浏览
- 格式如:"一是严格值班纪律。" "二是做好安全检查。" "三是加强检查。"
- 只加粗标题本身(到句号为止),解释说明不加粗
- 示例:一是严格值班纪律。 值班人员不得擅自离岗,遇紧急情况应及时报告带班领导。
- 每个"一是/二是/三是"应作为独立段落,避免加粗标题连在一起
- 英文数字使用 Times New Roman 字体
注意:方正小标宋字体需要单独安装。
段落格式
| 项目 | 参数 |
|---|
| 行距 | 固定值 28 磅 |
| 段前/段后 | 0 行 |
| 对齐方式 | 两端对齐(总标题居中,其他全部两端对齐) |
| 首行缩进 | 2 字符(约 0.74 cm),落款右对齐、无缩进 |
| 段落间隔 | 通过空行实现,不用段前段后间距 |
| 段前间距 | 根据标题级别设置 |
| 段后间距 | 根据标题级别设置 |
页码规范
- 位置:页面底端(页脚)
- 格式:— 1 —(四号宋体)
- 单页码居右,双页码居左
- 封面、目录、版记不标页码
落款日期
- 位置:正文下空若干行,右空四字
- 日期格式:阿拉伯数字,如 2026年2月1日
使用方法
Python代码调用
Word文档示例 - 公文格式
from editor import WordDocumentEditor, GONGWEN_FONTS, GONGWEN_PAGE
editor = WordDocumentEditor()
editor.add_heading("关于XXXX的通知", level=0)
editor.set_title("一、基本情况")
editor.set_title("二、工作要求", level=2)
editor.set_title("三、具体措施", level=3)
editor.add_paragraph("根据上级文件精神,结合本单位实际情况...")
editor.add_numbered_list(["第一项", "第二项", "第三项"], level=1)
editor.add_attachment_note(["1. XXX审批表", "2. YYY汇总表"])
editor.add_sender_info("XX部门", "2026年2月1日")
editor.save("公文模板.docx")
Excel表格示例
from editor import ExcelDocumentEditor
editor = ExcelDocumentEditor()
data = [
["序号", "姓名", "部门", "基本工资", "绩效奖金", "实发工资"],
["1", "张三", "技术部", "8000", "2000", "=D2+E2"],
["2", "李四", "市场部", "7500", "2500", "=D3+E3"],
]
editor.add_data(data)
editor.format_header(row=1,
font_name="黑体",
font_size=11,
bg_color="D9D9D9")
for i in range(1, 7):
editor.set_column_width(i, [8, 12, 12, 12, 12, 15][i-1])
editor.set_row_height(1, 20)
editor.add_borders("all")
editor.format_numbers(4, "#,##0.00")
editor.format_numbers(5, "#,##0.00")
editor.format_numbers(6, "#,##0.00")
editor.save("工资表.xlsx")
命令行使用
python3 /home/lxgxdx/.hermes/skills/openclaw-imports/document-editor/editor.py word -t '标题' -c '内容' -o output.docx
python3 /home/lxgxdx/.hermes/skills/openclaw-imports/document-editor/editor.py excel -d '姓名,年龄,城市' -o output.xlsx
高级功能
设置页面边距
editor.set_page_setup(top=3.7, bottom=3.5, left=2.8, right=2.6)
添加空行
editor.add_empty_line(count=2)
编号列表
editor.add_numbered_list(["第一项", "第二项"], level=1)
editor.add_numbered_list(["第一项", "第二项"], level=2)
editor.add_numbered_list(["第一项", "第二项"], level=3)
自定义段落格式
editor.add_paragraph(
"自定义内容",
font_name="微软雅黑",
font_size=Pt(12),
align="center",
bold=True,
first_line_indent=True,
line_spacing=Pt(24),
space_before=12,
space_after=12
)
实际应用场景
经验类信息稿(快速模板)
完整格式参数见 references/经验类信息稿格式.md
import sys
sys.path.insert(0, '/home/lxgxdx/.hermes/skills/ppt-work/document-work/document-editor')
from editor import WordDocumentEditor
from docx.shared import Pt
editor = WordDocumentEditor()
editor.set_page_setup(top=3.5, bottom=3.2, left=2.7, right=2.7)
editor.add_paragraph('标题', font_name='黑体', font_size=Pt(16), bold=True, align='center')
for para in ['第1段...', '第2段...']:
editor.add_paragraph(para, font_name='仿宋_GB2312', font_size=Pt(16),
bold=False, align='justify', first_line_indent=True, line_spacing=Pt(28))
editor.save('output.docx')
⚠️ font_size 和 line_spacing 必须用 Pt() 对象传入,不能用整数。传整数会导致字号变成 inherit(极小),文档看起来像空白。
⚠️ 中文字体必须双重指定:run.font.name='仿宋' 只控制西文字体,必须同时设置 run._element.rPr.rFonts.set(qn('w:eastAsia'), '仿宋') 才能让 Windows Word 正确渲染中文。editor.add_paragraph() 已自动处理此问题(2026-05-13修复),但裸写 python-docx 时必须手动添加。
场景1:制作正式公文
editor = WordDocumentEditor()
editor.set_page_setup()
editor.add_heading("关于做好2026年春节期间值班工作的通知", level=0)
editor.add_paragraph("各科室、全体员工:")
editor.add_paragraph("根据国务院办公厅通知精神,结合本单位实际情况,现将有关事项通知如下:", first_line_indent=True)
editor.set_title("一、放假时间")
editor.add_paragraph("2026年1月28日至2月4日放假,共8天。", first_line_indent=True)
editor.set_title("二、值班安排", level=2)
editor.add_paragraph("节日期间安排专人值班,值班电话:010-12345678。", first_line_indent=True)
editor.add_empty_line(2)
editor.add_sender_info("综合办公室", "2026年1月20日")
editor.save("值班通知.docx")
场景2:制作表格
editor = ExcelDocumentEditor()
data = [
["姓名", "部门", "基本工资", "绩效奖金", "实发工资"],
["张三", "技术部", "8000", "2000", "10000"],
["李四", "市场部", "7500", "2500", "10000"],
["王五", "财务部", "8500", "1800", "10300"],
]
editor.add_data(data)
editor.format_header(bg_color="4472C4", font_name="微软雅黑", font_size=11)
editor.set_column_width(1, 12)
editor.set_column_width(2, 12)
editor.set_column_width(3, 15)
editor.set_column_width(4, 15)
editor.set_column_width(5, 15)
editor.set_row_height(1, 22)
editor.add_borders("all")
editor.format_numbers(3, "#,##0.00")
editor.format_numbers(4, "#,##0.00")
editor.format_numbers(5, "#,##0.00")
editor.save("工资表.xlsx")
参考文件
references/经验类信息稿格式.md — 信息稿模板及参数
references/meeting-minutes-from-audio.md — 录音转会议记录完整流程(Whisper转写 → LLM整理 → Word生成)
常见问题
坑2:execute_code sandbox 不接受中文
问题:execute_code 的多行字符串传中文会报 SyntaxError: invalid character,原因是 sandbox 解析器对中文字符和中文标点(如""中文引号、、``。)直接拒绝。
正确做法:
- 用
write_file 将完整 .py 文件写入 /tmp/(含 UTF-8 中文原文字符串)
- 用
terminal 运行:python3 /tmp/gw_format.py
"""
editor.add_paragraph("五莲县...", font_name='黑体')
"""
问题现象:向 WordDocumentEditor 传入中文时,某些字符被静默替换为相似字:
\u83B2(莲)→ 莱
\u8BC9(诉)→ 请
\u805A(聚)→ 点
根本原因:python-docx 内部 OCR 式字符替换机制
解决方案:将中文文本写入 UTF-8 文件,从文件读取后再传给 WordDocumentEditor:
editor.add_paragraph("五莲县", font_name="黑体", font_size=14)
with open('/tmp/text.txt', 'w', encoding='utf-8') as f:
f.write("五莲县")
with open('/tmp/text.txt', 'r', encoding='utf-8') as f:
editor.add_paragraph(f.read(), font_name="黑体", font_size=14)
技术栈
- Word: python-docx 库
- Excel: openpyxl 库
- 字体: 支持所有系统已安装的中文字体
- 兼容性: 生成的文档可在Microsoft Office、WPS中打开