添加说明

main
liamxin 3 months ago
parent 746240af00
commit e30ed07f26

@ -0,0 +1,43 @@
[
{
"key": "{Title}",
"value": "测试标题",
"type": "text"
},
{
"key": "{文件内容}",
"value": "测试内容内容内容",
"type": "text"
},
{
"key": "{Table}",
"value": 123,
"type": "number"
},
{
"key": "{Picture}",
"value": "rust.jpg",
"type": "picture",
"width": 1.25
},
{
"key": "{Table.No}",
"value": "序号测试",
"type": "text"
},
{
"key": "{Table.Name}",
"value": "名称测试",
"type": "text"
},
{
"key": "{Table.Col2}",
"value": "列2",
"type": "text"
},
{
"key": "{Table.Col3}",
"value": "列3",
"type": "text"
}
]

Binary file not shown.

@ -1,47 +1,47 @@
import json
import sys
from docx import Document from docx import Document
from docx.shared import Inches from docx.shared import Inches
import sys
import json
def get_command_argv_by_sys(): def get_command_argv_by_sys():
# 默认值-测试 # 默认值-用于测试
template = "template.docx" template_path = "template.docx"
filename = "demo.docx" filename_path = "demo.docx"
datafile = "" datafile_path = "data.json"
# 读取参数
number = len(sys.argv) number = len(sys.argv)
if 2 == number: if 2 == number:
template = sys.argv[1] template_path = sys.argv[1]
if 3 == number: if 3 == number:
template = sys.argv[1] template_path = sys.argv[1]
filename = sys.argv[2] filename_path = sys.argv[2]
if 4 == number: if 4 == number:
template = sys.argv[1] template_path = sys.argv[1]
filename = sys.argv[2] filename_path = sys.argv[2]
datafile = sys.argv[3] datafile_path = sys.argv[3]
return (template, filename, datafile) return template_path, filename_path, datafile_path
def readData(datafile):
data = [ def read_data(filepath):
{"key": "{Title}", "value": "测试标题", "type": "text"}, content = []
{"key": "{文件内容}", "value": "测试内容内容内容", "type": "text"},
{"key": "{Table}", "value": 123, "type": "number"},
{"key": "{Picture}", "value": "A4.png", "type": "picture", "width": 1.25},
{"key": "{Table.No}", "value": "序号测试", "type": "text"},
{"key": "{Table.Name}", "value": "名称测试", "type": "text"},
{"key": "{Table.Col2}", "value": "列2", "type": "text"},
{"key": "{Table.Col3}", "value": "列3", "type": "text"},
]
if datafile != "": if datafile != "":
with open(datafile, "r", encoding="utf-8") as file: try:
data = json.load(file) with open(filepath, "r", encoding="utf-8") as file:
return data content = json.load(file)
finally:
return content
def replace(template, filename, data): def replace(template_path, filename_path, data_json):
docfile = Document(template) try:
document_file = Document(template_path)
except:
return 2
# 段落替换 # 段落替换
for paragraph in docfile.paragraphs: for paragraph in document_file.paragraphs:
for datum in data: for datum in data_json:
if datum["key"] in paragraph.text: if datum["key"] in paragraph.text:
if "text" == datum["type"]: if "text" == datum["type"]:
paragraph.text = paragraph.text.replace(datum["key"], datum["value"]) paragraph.text = paragraph.text.replace(datum["key"], datum["value"])
@ -52,10 +52,10 @@ def replace(template, filename, data):
paragraph.text = paragraph.text.replace(datum["key"], str(datum["value"])) paragraph.text = paragraph.text.replace(datum["key"], str(datum["value"]))
# 表格替换 # 表格替换
for table in docfile.tables: for table in document_file.tables:
for row in table.rows: for row in table.rows:
for cell in row.cells: for cell in row.cells:
for datum in data: for datum in data_json:
if datum["key"] in cell.text: if datum["key"] in cell.text:
if "text" == datum["type"]: if "text" == datum["type"]:
cell.text = cell.text.replace(datum["key"], datum["value"]) cell.text = cell.text.replace(datum["key"], datum["value"])
@ -65,14 +65,15 @@ def replace(template, filename, data):
paragraph.add_run().add_picture(datum["value"], width=Inches(datum["width"])) paragraph.add_run().add_picture(datum["value"], width=Inches(datum["width"]))
elif "number" == datum["type"]: elif "number" == datum["type"]:
cell.text = cell.text.replace(datum["key"], str(datum["value"])) cell.text = cell.text.replace(datum["key"], str(datum["value"]))
docfile.save(filename) document_file.save(filename_path)
return 0
if __name__ == '__main__': if __name__ == '__main__':
params = get_command_argv_by_sys() params = get_command_argv_by_sys()
template=params[0] [template, filename, datafile] = params
filename=params[1] data = read_data(datafile)
datafile=params[2] if len(data) == 0:
print(datafile) print(1)
data=readData(datafile) else:
print(data) print(replace(template, filename, data))
replace(template, filename, data)

@ -1,6 +1,18 @@
# 读取模板生成报表 # 读取模板生成报表
## 打包
### windows上直接使用以下命令打包为exe后缀的可执行文件
```powershell
pyinstaller -F main.py -w -n doc_generate.exe
```
## 使用
使用命令行调用
```powershell
```
```python
pyinstaller -F main.py -w
```
Loading…
Cancel
Save