gongwen-generator

Generate Word documents (.docx) that comply with the National Standard for party and government official document format (GB/T 9704-2012). Triggered when the user requests official documents or mentions related layout keywords (e.g., 'official document format', '28 characters per line 22 lines per page', 'Xiaobiaosong'), providing the corresponding page margins, font sizes, and document grid parameters for official-document formatting.

Installation

Review the source and dependencies with a prompt. A package is available to download.

Install with an AI assistant

Copy the prompt so your assistant can review the pinned revision and dependencies first.

View promptHide prompt
Check whether SkillPI Skill Finder is installed for your current agent. If it is missing, install and enable it using the official instructions at https://github.com/Findskills/skillpi. Then use Skill Finder to locate and install "gongwen-generator" from this exact SkillPI detail page: https://skillpi.com/skills/deepskill-1656ca57c741ee442b061f96a1d71422c19c1b2f5ea98d9c540d7081168df27f. Verify the upstream source and version against https://skillhub.cn/skills/user_c6ad3123/gongwen-generator before installing. If they do not match, stop and report the mismatch. Report the installed path and source version.

Source and setup

skillhub.cn

Setup guidance pending

See the original source for supported tools, dependencies, and permissions.

Installation test pending

Generate Word documents (.docx) that comply with the National Standard for party and government official document format (GB/T 9704-2012). Triggered when the user requests official documents or mentions related layout keywords (e.g., 'official document format', '28 characters per line 22 lines per page', 'Xiaobiaosong'), providing the corresponding page margins, font sizes, and document grid parameters for official-document formatting.

0 Downloads

Introduction source: third_party · user_c6ad3123 · 1.1.0 Reference

SkillPi interpretation

Tasks
Translation pending; see source documentation.
Testing
Not tested

Browse files

Browse files at the source ↗

Original SKILL.md

Open original ↗
Frontmatter
name: gongwen-generator
description: '生成符合《党政机关公文格式》国家标准(GB/T 9704-2012)的 Word 文档(.docx)。当用户要求制作公文、红头文件、机关/政府正式文档,或提到"公文格式""每行28字每页22行""小标宋""仿宋_GB2312""楷体_GB2312""黑体不加粗""版心""GB/T 9704"等关键词时触发。本 Skill 提供精确的页面边距、字体字号、文档网格(每页22行、每行28字)参数与生成脚本,覆盖标题、一级标题、二级标题、正文的完整排版。默认:只要用户要 Word 公文,优先用本 Skill 而非通用 docx 排版。不要用于一般商业报告或论文(除非用户明确要求公文格式)。'
license: MIT
agent_created: true
slug: gongwen-generator
version: 1.1.0
updated: 2026-07-30

党政机关公文格式(GB/T 9704-2012)Word 生成

本技能自带完整生成脚本 scripts/generate_gongwen.py,无需外部依赖(仅需目标机 pip install python-docx)。 本文档沉淀了 GB/T 9704-2012 公文排版的权威参数,以及作者在多次排错中总结的 OOXML 实现陷阱(见各节 ⚠️ 提示),可直接照抄落地。

何时使用

  • 用户要制作公文、红头文件、机关/政府正式文档。
  • 用户提到 "公文格式" / "每行28字每页22行" / "小标宋" / "仿宋_GB2312" / "楷体_GB2312" / "黑体不加粗" / "版心" / "GB/T 9704"。
  • 默认行为:任何 Word 公文类任务,优先调用本 Skill 的脚本,不要用手写样式或通用 docx 排版。

如何调用本技能

本技能已安装在用户级目录(跨项目可用)。三种调用方式:

  1. 自然语言直接说(最常用):在对话里提公文需求即可触发,例如 "帮我写一份 GB/T 9704 公文,标题是《……》"、 "生成一份通知,正文如下"、 "做个红头文件,每行 28 字每页 22 行"。只要出现"公文/红头/GB/T 9704/仿宋_GB2312/ 每行28字每页22行"等关键词,就会自动加载本技能生成。
  2. 显式点名:"用 gongwen-generator 生成公文……"。
  3. 给材料:可给 .txt 正文文件、直接粘贴正文,或让我按你口述起草。

输入约定:标题用 --title 或正文首行;一、二、 自动识别为一级标题(黑体), (一) 为二级(楷体),其余为正文(仿宋、首行缩进 2 字);--org / --date 设落款。

标准参数总表(权威值,照抄即可)

项目 国标要求 字体 字号 关键属性
纸张 A4 — — 210mm × 297mm
上边距(天头) 3.7 cm — — 对应 2098 twips
下边距 3.5 cm — — 对应 1985 twips
左边距(订口) 2.8 cm — — 对应 1588 twips
右边距 2.6 cm — — 对应 1474 twips
版心 156mm × 225mm — — 由边距+纸型推导
文档网格 每行 28 字、每页 22 行 — — 字符网格+行网格
标题 居中 方正小标宋简体 2 号(22pt) 不加粗
一级标题(一、) 左空二字(2字符) 黑体 3 号(16pt) 不加粗
二级标题((一)) 左空二字(2字符) 楷体_GB2312 3 号(16pt) 不加粗
正文 首行缩进 2 字符 仿宋_GB2312 3 号(16pt) 单倍行距(配文档网格锁定 22 行)
三级标题(1.) 左对齐 仿宋_GB2312 3 号 国标规定
四级标题((1)) 左对齐 仿宋_GB2312 3 号 国标规定

字号换算:二号=22pt、小二=18pt、三号=16pt、小三=15pt、四号=14pt、小四=12pt。

缩进:正文及一/二级标题均左空二字(2字符),与单位规范一致;国标主流做法为一/二级标题顶格,如需可改脚本常量 HEADER_FIRST_INDENT = False。

落款:--org 为署名单位、--date 为成文日期,二者均右空四字(距版心右边空 4 字符),符合 GB/T 9704 成文日期右空四字规定;落款与正文之间空两行;红头与红色反线不再生成(默认套印,无需电子排版)。

段落间距:正文与页码页脚段落段后=0、行距=单倍(1倍,line=240, lineRule=auto),段前强制为 0,正文空行被忽略(公文靠首行缩进分段),以保证严格每行28字、每页22行,不额外撑行。标题保留固定行距 33 磅(EXACTLY),不作改动。页码页脚段落必须显式写 w:spacing(after=0 + line=240 + lineRule=auto),否则会继承 Footer 样式默认间距。pPr 子元素顺序:…spacing, ind, …, jc…。

段落「换行和分页」:已关闭『孤行控制』(widow/orphan control)(在 Normal 样式级与每个段落 pPr 均写 w:widowControl w:val="false")。公文转页时不应因孤行控制而强行调整,落款/条款的末行可自然落到下页。如需重新开启,在 scripts/generate_gongwen.py 中移除 _disable_widow_control 的调用即可。

对齐方式:正文、一/二级标题、主送机关默认为两端对齐(JUSTIFY);标题居中;落款(署名/日期)右对齐(右空四字)。_new_para 的 align 默认即为 WD_ALIGN_PARAGRAPH.JUSTIFY,调用方无需单独指定;如需其他对齐再显式传入 align 参数。

页码(GB/T 9704-2012 规范):4号宋体阿拉伯数字,数字左右各一条一字线(U+2500 BOX DRAWINGS LIGHT HORIZONTAL,─,WPS「页码格式」第 3 种样式),双面外侧(奇数页右、偶数页左)。实现:

  • evenAndOddHeaders 必须写入 word/settings.xml(OOXML §17.15.1.27,CT_Settings 成员)。错放到 sectPr 中 WPS/严格校验会忽略,导致所有页都走奇数页(右侧)。python-docx 写法:doc.settings.element.append(OxmlElement('w:evenAndOddHeaders'))。
  • 一字线字符用 ─ (U+2500),不要用 — (U+2014 EM DASH) — 后者在部分中文字体下宽度偏短,达不到一字线视觉效果。
  • section.footer(default,奇数页)右对齐、section.even_page_footer(even,偶数页)左对齐,两侧页脚段落内容均为「─ + PAGE 域 + ─」。
  • 段落对齐:需在 pPr 显式写 w:jc(不能依赖 pStyle 继承),奇数 w:val="right"、偶数 w:val="left"。
  • 段落缩进(双写法,避免 WPS 渲染丢字符单位):w:ind 同时设 w:leftChars="100" + w:left="280"(文本之前/左,1 字符)、w:rightChars="100" + w:right="280"(文本之后/右,1 字符);注意必须用 leftChars(左缩进)而非 firstLineChars(首行缩进),否则奇偶页外侧"空一字"语义不对(4号 1 字符 = 14pt = 280 twips)。
  • 页脚段落必须显式写 w:spacing(段后=0 + 单倍行距 line=240 lineRule=auto),否则继承 Footer 样式默认间距。 代码:generate_gongwen.py::add_page_numbers(doc)(已内置,生成即带正确页码)。存量文档可用 C:\Users\Administrator\Desktop\八百标兵奔北坡\.workbuddy\fix_pgnum_full.py 的 fix_page_number(path) 重做。

⚠️ 页码字体致命陷阱(必读):设置 run 字体时,必须同时把 w:ascii、w:hAnsi、w:cs、w:eastAsia 四个属性都设为「宋体」;只设 w:eastAsia=宋体 而让 w:ascii/w:hAnsi/w:cs 取默认(通常是 Times New Roman),会导致页码的阿拉伯数字(ASCII 字符)以 Times New Roman 显示——这是反复出错的坑。中文公文页码数字也用宋体,四个属性统一写「宋体」即可。

⚠️ PAGE 域结构必须完整:标准结构为 begin → instrText(" PAGE ") → separate → 结果占位run(如"1") → end,缺一不可。缺 separate 或结果 run 时,部分渲染器算不出实际页码、或结果字体回退为默认(又变 Times New Roman)。结果占位 run 也要显式设宋体。

页眉/页脚距(用户硬性要求):section.header_distance = 1.0cm(页眉上边距)、section.footer_distance = 2.9cm(页脚下边距,页码位于版心下边缘之下约 7mm)。对应 w:pgMar 的 w:header / w:footer 字段。Word 默认值均为 1.27cm(12.7mm),务必显式设置,否则页码贴底、且页眉区位置不对。 ⚠️ pPr 子元素顺序(缩进不生效的隐藏坑):段落 pPr 内 w:ind 必须写在 w:jc 之前(OOXML 顺序 …spacing, ind, …, jc…)。若先写 jc 再写 ind,WPS/严格校验会丢弃 w:ind,导致"文本之前/之后1字符"缩进不显示。脚本已按正确顺序写入(见 _set_pgnum_para)。

与你口语描述的核对 + 易错点

你的描述(标题2号小标宋、一级黑体不加粗、二级楷体_GB2312、正文仿宋_GB2312、28字/22行、边距3.7/3.5/2.8/2.6)完全正确,贴合国标 ✅。

之前排版反复出问题的根因不在你说得不清楚,而在实现端:

  1. 你漏说了"正文字号" —— 国标正文必须是 3 号(16pt)。一级/二级标题也是 3 号。 之前只要正文没锁死 3 号(用了小四/四号/默认字号),"28字/行、22行/页"立刻算错,整篇全乱。

  2. 字体在"打开文档的电脑"上不存在 —— 方正小标宋简体、仿宋_GB2312、楷体_GB2312 在 Win7+ / Linux / macOS 上大多默认没有(Win7 起用"仿宋""楷体"替代,缺 _GB2312 后缀)。 生成端写对了字体名,但用户机器没有 → Word 用替代字体渲染,字宽变了 → 每行字数、每页行数全错。 必须:①在文档里写入正确的 eastAsia 字体名;②提醒用户在 Office 机器安装这些字体。

  3. "行间距"不能直接达标 —— 只设"1.5倍行距/固定值"不够。必须三件套一起: 文档网格(指定行和字符网格, 22行×28字) + 段落对齐网格(snapToGrid):正文单倍行距、标题固定33磅,配合 snapToGrid 锁定 22 行; 才精确锁死版心。本脚本已内置这三件套。

  4. 中文字体必须写 eastAsia —— 否则 docx 只套西文字体,中文设置不生效(最常见的"设置了却没变"原因)。

  5. w:docGrid 的 charSpace 单位是「1/4096 磅」且是相对 Normal 字号(16pt)的增量 —— 这是 28字/行 最难踩的坑,曾反复栽在这里。Word 对话框的"每行"数字 = floor(版心宽 ÷ 网格字符间距), 而网格字符间距(pt) = Normal字号(16) + charSpace/4096。要让 28 字放得下,间距必须 ≤ 315.95 twips (版心8845 ÷ 28)。所以:

    • ❌ 把 charSpace 当 twips 直接写 315 → 实际只 +0.077pt,间距仍≈320twips → 截成 27 字。
    • ✅ 设负值增量:charSpace = (目标间距pt − 16) × 4096,取目标≈312twips(15.6pt)得 -1638, 间距压到 312twips → 8845 ÷ 312 = 28.3 → 28 字。linePitch 同理(576twips→22行)。
    • 此法与字体实际字宽无关(只要字体全角 ≤ 16pt,所有仿宋克隆/正版都满足),故正文用标准 仿宋_GB2312 即可,不必再搞字体补丁/改名等 workaround。
    • 公式已在 scripts/generate_gongwen.py::set_doc_grid 实现,调用方无需关心。

使用脚本

脚本路径:scripts/generate_gongwen.py(依赖 python-docx;目标机需先 pip install python-docx,本机不保证预装)。

# 方式一:纯文本文件生成(自动识别 一、=一级标题 (一)=二级标题 其余=正文)
python3 scripts/generate_gongwen.py \
  --title "关于开展XXX工作的通知" \
  --input body.txt \
  --output 公文.docx \
  # 落款单位(可省略,默认即为铜川市农业农村局)
  --org "铜川市农业农村局" \
  --date "2026年7月27日"

# 方式二:JSON 配置(精细控制每个块的类型)
# blocks 支持 type: title | recipient | level1 | level2 | req | body
# req = 工作要求类内嵌标题:同一段落内标题(head)用楷体_GB2312、续接正文(text)用仿宋_GB2312,
#       整段两端对齐、首行缩进2字。适用于"三、工作要求"下(一)~(六)这类标题与内容同行的情况。
# recipient = 主送机关(顶格、仿宋_GB2312 三号、无首行缩进,文本自带全角冒号,
# 置于标题后第一个块),适用于函/通知等需要主送机关的文种
python3 scripts/generate_gongwen.py --config cfg.json --output 公文.docx

# 方式三:作为模块被调用
from scripts.generate_gongwen import build_gongwen
build_gongwen("公文.docx", title="...", blocks=[{"type":"body","text":"..."}])

文本文件规则(--input):

  • 第一行作为标题(由 --title 覆盖)。
  • 行首为 一、二、…十、 → 一级标题(黑体不加粗)。
  • 行首为 (一) 或 (一) → 二级标题(楷体_GB2312)。
  • 其余非空行 → 正文(仿宋_GB2312,首行缩进2字符)。
  • 空行 → 分段。

字体兜底:默认不回退,原样写入国标字体名(保证在装了字体的 Office 机器上 100% 合规)。 若生成端也想直接预览,加 --fallback 会把缺失字体映射到近似字体(仿宋_GB2312→仿宋、 楷体_GB2312→楷体、方正小标宋简体→宋体)并给出告警。

验证产出合规

生成后用以下命令回读校验关键 XML 参数(页边距、字体、文档网格),确认与国标一致:

python3 - <<'PY'
from docx import Document
from docx.shared import Cm
d = Document("公文.docx"); s = d.sections[0]
print("页边距 cm:", s.top_margin.cm, s.bottom_margin.cm, s.left_margin.cm, s.right_margin.cm)
print("docGrid:", s._sectPr.find('{http://schemas.openxmlformats.org/wordprocessingml/2006/main}docGrid').attrib)
PY

字体安装提示(交付给用户)

若打开后字体不对,需在 Windows 安装:方正小标宋简体、仿宋_GB2312、楷体_GB2312 (或确认系统已有对应字体)。可调用 gongwen-fonts 技能一键安装这四款国标字体(含被锁文件排期重启兜底)。 Linux/macOS 预览可装 wqy/文泉驿等近似字体,但终稿建议在 Windows + Office 校验。

注:哪怕没装这几款字体,本技能生成的文档网格仍是 28 字/22 行(由 charSpace 负值锁定), 只是字形会变成替代字体;装上国标字体后字形才完全合规。

Current listing version

1.1.0

Verify the source revision ↗

Safety and assessment details

Quality assessment

Out of 100 · Assessment report from SkillPi LAB.

Scenario Value92/100

Highlights

  • 精准解决党政机关公文生成刚需,符合 GB/T 9704-2012 国家标准
  • 提供完整脚本与权威参数沉淀,显著提升政府/事业单位文档制作效率
Trigger Precision95/100

Highlights

  • 触发词覆盖全面且边界清晰,包含关键词如'公文格式''每行28字每页22行''GB/T 9704'等
Structure and Standards93/100

Highlights

  • 文档结构完整,含使用场景、调用方式、参数表、脚本示例与验证方法
  • frontmatter 完整,步骤清晰,提供端到端示例
Execution Reliability91/100

Highlights

  • 工具依赖明确(python-docx),参数实现严谨,覆盖页码、字体、网格等关键陷阱
  • 提供字体安装兜底方案与文档校验方法
Evidence Verifiability (mean of the first four)93/100

The arithmetic mean of the first four original scores, rounded to a whole number.

Static text checks

100 / 100 · skillpi-20261123-no-r3-v1

No current rules matched the text.

This score reflects rule matches in the text and is provided for reference before use. It excludes attachments and runtime behavior.

Permissions and checks before use

Automatically summarized from this main document. Matches may be examples or defensive advice; they are not actual permission declarations. Attachments, external dependencies and runtime behavior are not covered.

  • Install or fetch external dependencies

    Use official sources and pin versions. Check install scripts in an isolated environment. This document check does not cover code downloaded later.

    Mentioned in main document · lines 13, 112
  • Administrator or elevated privileges

    Elevate only when necessary. Keep protection and certificate validation enabled and avoid broad system access.

    Mentioned in main document · lines 72
Domains or addresses referenced in the document

These addresses may be documentation links or examples, not an approved network allowlist. Verify actual destinations and outgoing data before use.

  • schemas.openxmlformats.org

These notices grant no permissions. Your host tool must still confirm file changes, secrets, external transfers or costs as needed.

Security rating and access scope

safe · Static evidence review; not a runtime safety guarantee.

Before use, confirm and authorize actual file access, external services and network destinations in your host. This review grants no runtime permissions.