API 参考文档
strict_word_filler 引擎所有公开 API 详细说明
1. 模块索引
| 模块 | 文件 | 功能 |
|---|---|---|
| pipeline | strict_word_filler/pipeline.py |
CLI 参数解析与执行入口 |
| loader | strict_word_filler/loader.py |
JSON 解析与 BuildPlan 构建 |
| docx_ops | strict_word_filler/docx_ops.py |
Word 文档操作 |
| models | strict_word_filler/models.py |
不可变数据模型 |
| errors | strict_word_filler/errors.py |
异常类型定义 |
2. pipeline 模块
main
CLI 主入口。
1 | def main(argv: list[str] | None = None) -> int |
参数
| 参数 | 类型 | 说明 |
|---|---|---|
argv |
list[str] | None |
命令行参数,默认使用 sys.argv |
返回值
| 类型 | 说明 |
|---|---|
int |
0 表示成功 |
CLI 参数
| 参数 | 说明 | 必需 |
|---|---|---|
--template |
Word 模板文件路径 | 是 |
--config |
config.json 文件路径 | 是 |
--project |
project.json 文件路径 | 是 |
--output |
输出 .docx 文件路径 | 是 |
示例
1 | python main.py \ |
3. loader 模块
load_json
加载 JSON 文件。
1 | def load_json(path: Path) -> dict |
参数
| 参数 | 类型 | 说明 |
|---|---|---|
path |
Path |
JSON 文件路径 |
返回值
| 类型 | 说明 |
|---|---|
dict |
解析后的字典 |
异常
| 异常 | 说明 |
|---|---|
FileNotFoundError |
文件不存在 |
json.JSONDecodeError |
JSON 格式错误 |
build_plan
从配置数据构建不可变的 BuildPlan。
1 | def build_plan(config_data: dict, project_data: dict) -> BuildPlan |
参数
| 参数 | 类型 | 说明 |
|---|---|---|
config_data |
dict |
config.json 内容 |
project_data |
dict |
project.json 内容 |
返回值
| 类型 | 说明 |
|---|---|
BuildPlan |
包含所有替换规则的不可变构建计划 |
异常
| 异常 | 说明 |
|---|---|
StrictDataError |
JSON 缺少必填字段或字段为空 |
说明
处理流程:
- 从
config_data提取封面字段 → 生成ExactReplaceRule - 遍历
project_data["structure"]→ 生成各章节 Rule - 遇到
placeholders中含subsections的动态章节 →DynamicSectionRule - 遇到
tables→TableFillRule - [11] 遇到
cscis→CSCISectionRule
4. docx_ops 模块
apply_plan
总入口,按顺序执行 BuildPlan 中所有规则。
1 | def apply_plan(doc: DocumentObject, plan: BuildPlan) -> None |
参数
| 参数 | 类型 | 说明 |
|---|---|---|
doc |
DocumentObject |
python-docx Document 对象 |
plan |
BuildPlan |
构建计划 |
执行顺序
exact_replacements— 精确替换dynamic_sections— 动态章节paragraph_replacements— 段落替换csci_sections— CSCI 章节(仅 [11])tables— 表格填充- 图表编号
- TOC 标记
apply_exact_replacement
精确匹配段落文本并替换。
1 | def apply_exact_replacement( |
参数
| 参数 | 类型 | 说明 |
|---|---|---|
doc |
DocumentObject |
文档对象 |
old_text |
str |
待替换的精确文本 |
new_text |
str |
替换后文本 |
expected_matches |
int |
期望命中的段落数量 |
key |
str |
规则标识(用于错误信息) |
异常
| 异常 | 说明 |
|---|---|
StrictTemplateError |
命中数量不等于 expected_matches |
apply_paragraph_replacement
包含匹配段落文本并替换,支持多行展开。
1 | def apply_paragraph_replacement( |
参数
| 参数 | 类型 | 说明 |
|---|---|---|
doc |
DocumentObject |
文档对象 |
anchor_text |
str |
锚点文本(包含匹配) |
new_text |
str |
替换后文本(支持 \n 分行) |
expected_matches |
int |
期望命中数量(默认 1) |
key |
str |
规则标识 |
apply_dynamic_section
克隆原型段落生成可变数量子章节。
1 | def apply_dynamic_section(doc: DocumentObject, rule: DynamicSectionRule) -> None |
说明
- 定位父标题、摘要段落、结束锚点、原型标题/正文段落
- 删除清理锚点之间的模板内容
- 为每个
DynamicSectionItem克隆原型段落并设置标题和正文 - 按反向顺序删除被替换的模板段落
apply_csci_sections
[11] 专属。克隆整个 CSCI 原型块。
1 | def apply_csci_sections(doc: DocumentObject, rule: CSCISectionRule) -> None |
说明
- 定位 13 个锚点段落(原型标题、概述、部件、执行、接口等)
- 提取从原型标题到结束锚点之间的所有段落作为原型范围
- 删除原型范围
- 为每个
CSCISectionItem克隆整个原型范围,逐段修改标题和内容
apply_table_fill
按 locator_rows 定位表格并填充数据。
1 | def apply_table_fill(doc: DocumentObject, rule: TableFillRule) -> None |
说明
- 遍历所有表格,匹配
locator_rows(头部行精确匹配) - 若数据行不足,克隆最后一行数据行原型
- 逐行逐格写入内容
- [11] 版本额外清除多余数据行并处理合并单元格
apply_caption_numbering
自动编号图表标题。
1 | def apply_caption_numbering(doc: DocumentObject) -> None |
说明
匹配格式 表X-X / 图X-X 的占位符,按章号递增编号。正则:^(表|图)(\d+|X)-X(.*)$
mark_toc_for_update
设置 Word 文档的 updateFields 标记。
1 | def mark_toc_for_update(doc: DocumentObject) -> None |
遍历辅助函数
| 函数 | 签名 | 说明 |
|---|---|---|
iter_all_paragraphs |
(doc) -> Generator[Paragraph] |
遍历所有段落(含页眉页脚、表格内嵌) |
iter_body_non_toc_paragraphs |
(doc) -> Generator[Paragraph] |
遍历正文段落,跳过 TOC 样式 |
iter_container_paragraphs |
(container) -> Generator[Paragraph] |
遍历容器段落(递归处理表格) |
find_single_body_paragraph |
(doc, text, *, exact) -> Paragraph |
查找唯一匹配的正文段落 |
paragraph_index |
(doc, paragraph) -> int |
获取段落在正文中的索引 |
set_paragraph_text |
(paragraph, text) -> None |
保留首个 run 格式设置文本 |
set_paragraphs_from_text |
(paragraph, text) -> None |
多行文本展开为新段落(克隆原型格式) |
delete_paragraph |
(paragraph) -> None |
删除段落 XML 节点 |
5. models 模块
通用模型
1 |
|
[11] 专属模型
1 |
|
6. errors 模块
1 | class StrictTemplateError(RuntimeError): |