Word report helpers with page numbering, table of contents, and Chinese uppercase number conversion.
Project description
wc_word_report_tool
一个可独立上传到 PyPI 的小包,包含两部分:
number_to_chinese_upper():把数字转成中文金额大写。WordFormatter:基于python-docx的常用 Word 格式化方法。
安装
pip install wc_word_report_tool
数字转大写
from wc_word_report_tool import number_to_chinese_upper
print(number_to_chinese_upper(123456.78))
# 壹拾贰万叁仟肆佰伍拾陆元柒角捌分
命令行也可以直接用:
wc-rmb-upper 100200.03
Word 方法
from docx import Document
from wc_word_report_tool import WordFormatter
doc = Document()
WordFormatter.set_all_layout(doc)
WordFormatter.fengmian_doc1(doc, "测试项目")
WordFormatter.Heading_1(doc, "1. 概述")
WordFormatter.Normal_doc(doc, "这是正文。")
doc.save("demo.docx")
页码从指定位置重新按 1 开始
Word 里“页码重新从 1 开始”本质上要靠“分节”实现。
from docx import Document
from wc_word_report_tool import WordFormatter
doc = Document()
WordFormatter.Normal_doc(doc, "前置内容,不参与新的页码计数")
WordFormatter.insert_section_with_page_numbering(
doc,
start_page_number=1,
prefix="第",
suffix="页",
alignment="居中",
)
WordFormatter.Heading_1(doc, "1. 正文开始")
WordFormatter.Normal_doc(doc, "这里所在的节,页码会从 1 开始。")
doc.save("page_number_demo.docx")
如果你已经自己建好了分节,也可以直接对某个 section 调:
section = doc.sections[1]
WordFormatter.restart_page_numbering(section, start=1)
生成目录并定义目录级别样式
from docx import Document
from wc_word_report_tool import WordFormatter
doc = Document()
WordFormatter.add_toc(
doc,
title="目录",
levels=(1, 3),
toc_level_styles={
1: {"font_name": "黑体", "font_size": 14, "bold": True, "space_after": 6},
2: {"font_name": "仿宋_GB2312", "font_size": 12, "left_indent": 24},
3: {"font_name": "宋体", "font_size": 11, "left_indent": 48},
},
)
WordFormatter.Heading_1(doc, "1. 一级标题")
WordFormatter.Heading_2(doc, "1.1 二级标题")
WordFormatter.Heading_3(doc, "1.1.1 三级标题")
doc.save("toc_demo.docx")
也可以单独设置某一级目录样式:
WordFormatter.set_toc_level_style(
doc,
1,
font_name="黑体",
font_size=14,
bold=True,
)
如果你不是用内置 Heading 1/2/3,也可以按自定义段落样式进目录:
WordFormatter.set_paragraph_style(
doc,
"MyHeading1",
font_name="黑体",
font_size=14,
bold=True,
)
WordFormatter.add_toc(
doc,
custom_style_levels={"MyHeading1": 1},
use_outline_levels=True,
)
WordFormatter.add_custom_heading(
doc,
"这是自定义样式标题",
style_name="MyHeading1",
level=1,
)
注意
python-docx可以把目录域和页码域写进.docx,但目录内容通常需要在 Word 里打开文档后更新一次。- “从第几页开始重新编号”在技术上是“从某个分节开始重新编号”。如果你说的是按最终排版后的物理页自动识别第 N 页,这件事
python-docx本身不擅长,建议在目标位置手动插入分节,或者在生成逻辑里明确分节点。 - 当前我已经补了最小测试文件在
tests/test_word_formatter.py,如果你本机装了pytest,可以直接跑:cd wc_word_report_tool && PYTHONPATH=src python3 -m pytest tests/test_word_formatter.py -q
打包上传
目录里已经带了 publish_to_pypi.sh,典型流程:
cd wc_word_report_tool
python3 -m pip install --upgrade build twine
./publish_to_pypi.sh
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
wc_word_report_tool-0.1.1.tar.gz
(11.9 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file wc_word_report_tool-0.1.1.tar.gz.
File metadata
- Download URL: wc_word_report_tool-0.1.1.tar.gz
- Upload date:
- Size: 11.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8179c7e26328086144b2ec2e1d4e963cfafb3aed6c38ad4193ed20715c562d36
|
|
| MD5 |
cf1b1a13d9e35b9dfc4d490cf08eb963
|
|
| BLAKE2b-256 |
0c5b9062babad3788b01f71cf6ca8fa157953d208e315188d7febaa611cdc49a
|
File details
Details for the file wc_word_report_tool-0.1.1-py3-none-any.whl.
File metadata
- Download URL: wc_word_report_tool-0.1.1-py3-none-any.whl
- Upload date:
- Size: 10.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9d6db1ff87d6b06d074ad01b0b323b76e96851d9a50fbcae6df7110f7bdaff86
|
|
| MD5 |
c27da28d5fcf51ec715a569b265de592
|
|
| BLAKE2b-256 |
5aea59ded9ead4928c3b3aa83235268a928d9d9c3fa48b6bd6b2251e547246d6
|