Skip to main content

cn-gov-docx

PyPI version Python

cn-gov-docx是一个基于 python-docx 的中文公文 Word(.docx)生成工具,用于快速生成符合政府机关常见排版规范的文档结构。

本项目目标是提供一套稳定、可复用的公文排版生成层,减少重复排版工作。


功能特点

  • 自动生成 A4 公文版式(页边距已预设)
  • 内置中文公文常用字体规则
  • 支持标题 / 分级标题 / 正文结构
  • 自动处理首行缩进与行距
  • 封装常用 Word 样式设置逻辑
  • 基于 python-docx,无复杂依赖

安装

pip install cn-gov-docx

快速开始

from cn_gov_docx import CnGovDocx

doc = CnGovDocx()

doc.add_title("关于推进重点工作的通知")

doc.add_h1("一、总体要求")
doc.add_text("这里是正文内容,用于测试公文排版效果。")

doc.add_h2("(一)工作目标")
doc.add_text("继续补充正文内容。")

doc.save("output.docx")

默认排版规则

页面设置(A4)

  • 上边距:3.7 cm
  • 下边距:3.5 cm
  • 左边距:2.8 cm
  • 右边距:2.6 cm

字体规范

类型 字体
标题 方正小标宋简体
一级/二级/三级标题 黑体
正文 仿宋_GB2312

正文格式

  • 字号:16pt(接近三号)
  • 行距:28pt
  • 首行缩进:2字符
  • 对齐方式:两端对齐

API

CnGovDocx()

创建文档实例。

doc = CnGovDocx()

add_title(text)

添加居中主标题。

doc.add_title("标题")

add_h1(text)

添加一级标题,同样的还有二、三级标题。

doc.add_h1("一级标题")
doc.add_h2("二级标题")
doc.add_h3("三级标题")

add_text(text, indent_first_line=True, remove_newlines=True)

添加正文内容。

doc.add_text("正文内容")

参数

  • indent_first_line:是否首行缩进(默认 True)
  • remove_newlines:是否移除换行并合并为单段(默认 True)

针对remove_newlines的说明

Python 三引号换行通常用于代码可读性,不代表真实段落结构。该参数用于区分“格式化换行”与“真实排版结构”。

True(默认)
  • 忽略所有换行符(含 """...""" 内换行)
  • 合并为单一段落
  • 用于公文正文标准排版
False
  • 保留原始换行结构
  • 每行按原样写入文档
  • 可能形成多段或显式换行

save(path)

保存文件。

doc.save("result.docx")

使用说明(重要)

1. 字体依赖

本项目依赖系统字体:

  • 方正小标宋简体
  • 黑体
  • 仿宋_GB2312

如果系统缺失字体,Word 会自动替换字体,可能导致排版不一致。


2. 适用范围

适用于:

  • 政府公文格式草稿
  • 国企/事业单位文书
  • 标准化报告生成
  • 批量文档生成场景

不适用于:

  • 精确符合某地区红头文件模板(需二次定制)
  • 复杂 Word 版式设计
  • 非中文排版场景

依赖

  • python-docx >= 1.1.0 (别的版本没有测试)
  • lxml >= 5.0

未来计划

  • 支持图片插入
  • 更严格的 GB/T 9704 对齐

Metadata

Release files for cn-gov-docx 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for cn-gov-docx 0.2.1
File Size Uploaded Provenance Actions
cn_gov_docx-0.2.1.tar.gz 7.4 kB Without provenance Details for cn_gov_docx-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cn-gov-docx 0.2.1
File Interpreter ABI Platform Actions
cn_gov_docx-0.2.1-py3-none-any.whl Python 3 none any Details for cn_gov_docx-0.2.1-py3-none-any.whl

Total release size: 14.7 kB

Release files / cn_gov_docx-0.2.1.tar.gz

Download URL cn_gov_docx-0.2.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
8f29191d6987a7dad5765cf275238825fa4db2489f80dcb396a363023cc66930
BLAKE2b-256 checksum
How to use checksums
41a12f2238bb846ef8d675e9fe80b358dfc6b606ea1cfaa1a75df85423307ee0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / cn_gov_docx-0.2.1-py3-none-any.whl

Download URL cn_gov_docx-0.2.1-py3-none-any.whl
Size 7.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
16cdeb13fcbc46633694c4e29ffaf9b5f5c2ed15d38ca81e5a74468f6232d72d
BLAKE2b-256 checksum
How to use checksums
4a3a6dbac0bc6d69988a953dcf2b1905be36ab6d1fa18539bfb32413d3a64c65
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.11.9 {"installer":{"name":"uv","version":"0.11.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for cn-gov-docx 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page