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.0.tar.gz
(11.8 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.0.tar.gz.
File metadata
- Download URL: wc_word_report_tool-0.1.0.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d98b794729c30e4c0f66463472b701b9278984b7482b1e7d2ea22f01a02794bb
|
|
| MD5 |
52860dad06b42208ebbdf2efa163eae2
|
|
| BLAKE2b-256 |
d61da0948c482d0acc199da983ae5809ed7593d6233f0182ca55de31217bf55d
|
File details
Details for the file wc_word_report_tool-0.1.0-py3-none-any.whl.
File metadata
- Download URL: wc_word_report_tool-0.1.0-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 |
96b638e549c7cf0d8ef0faf7b0955d58c587a6cbc20ded45aa98450876055105
|
|
| MD5 |
0c05d357948aded3c5427fd11a1d1773
|
|
| BLAKE2b-256 |
ee877ce05eaafc7e69415c17574ce391d692854ccf65b78a6e4a8a77e1d109ae
|