Skip to main content
# 文本模板转DOCX文件
[![PyPI](https://img.shields.io/pypi/v/docx-parsing-gmdzy2010.svg)](https://pypi.org/project/docx-parsing-gmdzy2010/)

### 功能概述
_________
本包基于一套自定义的`轻量级文本模板解析系统`,用于将文本文件转成Word的docx文件

#### 输入文件
###### 1. 文本模板文件
模板文件是一个纯文本文件,每3行为一个解析单位,解析规则只有以下3条:
>1.1 第一行包括元素类型和样式名称,用":"隔开;

>1.2 第二行为要写入docx文件的文本,每行内部分为多个run单位,用"||"隔开,每个run可以有下划线和加粗两种样式,在每个run的尾部,用"::"隔开

>1.3 第三行为分割线,79个下划线,为了让模板更易读而已,没太大意义

###### 2. DOCX模板文件
文件为一个空的或者自定义的docx文件,最好是包含python-docx自带docx模板的所有样式,方便直接调用
注意:样式有可能不生效,解决方法请移步python-docx的官方文档

###### 3. 文本模板的上下文数据
上下文数据为一个字典型的变量,用来对模板的变量进行格式化。

3.1 段落上下文对象`PARAGRAPH_CONTEXT`

例如,文本模板中有一个段落上下文变量`test_context`
```
...
cover:cover_title
模板内容||{test_context}::underline||后续文本
-------------------------------------------------------------------------------
...
```
则需要新建段落上下文对象,是一个python字典对象,可以以字典键值对的形式添加多个

```python
PARAGRAPH_CONTEXT = {
"demo_field_1": "测试文本",
"demo_field_2": 300.00,
"demo_field_3": 18003.00,
}

```
3.2 表格上下文对象`TABLE_CONTEXT`

例如,文本模板中有一个表格
```
...
table:demo_table_1
表格下面的文本不重要,仅仅用于占位
-------------------------------------------------------------------------------
...
```
这时需要新建表格上下文对象,包含表格的维度`rows`, `cols`,样式`style`等,类似于下方,可以以字典键值对的形式添加多个
```python
TABLE_CONTEXT = {
"demo_table_1": {
"attr": {
"style": "Table Grid",
"rows": 5,
"cols": 4,
},
"data": (
("第1列标题", "第2列标题", "第3列标题", "第4列标题"),
("第1行第1列内容", "第1行第2列内容", "第1行第3列内容", "第1行第4列内容"),
("第2行第1列内容", "第2行第2列内容", "第2行第3列内容", "第2行第4列内容"),
("第3行第1列内容", "第3行第2列内容", "第3行第3列内容", "第3行第4列内容"),
("第4行第1列内容", "第4行第2列内容", "第4行第3列内容", "第4行第4列内容"),
)
},
},
```
3.3 图片上下文对象`PICTURE_CONTEXT`

仍在测试中,后续单元测试通过后会将文档更新

### 使用
_________
>STEP 1. 在项目中导入渲染器`DocxProduce`,

>STEP 2. 写好文本模板,选择一个自带样式比较齐全的DOCX文件,可以从python-docx默认的模板拷贝而来

>STEP 3. 加载准备好的上下文和格式及样式数据,执行程序即完成


Release files for docx-parsing-gmdzy2010 1.3.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 docx-parsing-gmdzy2010 1.3.1
File Size Uploaded
docx_parsing_gmdzy2010-1.3.1.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for docx-parsing-gmdzy2010 1.3.1
File Interpreter ABI Platform
docx_parsing_gmdzy2010-1.3.1-py3-none-any.whl Python 3 none any Details

Total release size: 18.3 kB

Release files / docx_parsing_gmdzy2010-1.3.1.tar.gz

Download URL docx_parsing_gmdzy2010-1.3.1.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
a3dc83dc335d5576493865d98676d3032d5ad4e7585eb55d2ff13fc0f7600c44
BLAKE2b-256 checksum
How to use checksums
c02c6702bc208f50c25085951ebb9e04868478a3e111a61ce298c7c83524bb8c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.12.4 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.0

Release files / docx_parsing_gmdzy2010-1.3.1-py3-none-any.whl

Download URL docx_parsing_gmdzy2010-1.3.1-py3-none-any.whl
Size 10.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dd9d6e97aac90817cb29884fdcbec2612163656d541c51e829a2691640241f68
BLAKE2b-256 checksum
How to use checksums
e70d7141a9e3a7a80dec61a5873afd49784f17ea02de9af25644fba154bb7c2f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.12.4 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.6.0

Release history Release notifications | RSS feed

This release

1.3.1 This release

2 release files

1.3.0

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

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