From 746240af00e5096cbdc2b9090fe5a0dc1787bb9a Mon Sep 17 00:00:00 2001 From: liamxin Date: Wed, 12 Feb 2025 22:33:28 +0800 Subject: [PATCH] add --- .gitignore | 1 + help.pyx | 33 --------------------------------- main.py | 49 +++++++++++++++++++++++-------------------------- readme.md | 7 ++++++- setup.py | 15 --------------- 5 files changed, 30 insertions(+), 75 deletions(-) delete mode 100644 help.pyx delete mode 100644 setup.py diff --git a/.gitignore b/.gitignore index e2cae22..9dddaf7 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /build/ /dist/ +.idea \ No newline at end of file diff --git a/help.pyx b/help.pyx deleted file mode 100644 index 18f621f..0000000 --- a/help.pyx +++ /dev/null @@ -1,33 +0,0 @@ -from docx import Document -from docx.shared import Inches - -def replace(template ,filename, data): - doc = Document(template) - # 段落替换 - for paragraph in doc.paragraphs: - for datum in data: - if datum["key"] in paragraph.text: - if "text" == datum["type"]: - paragraph.text = paragraph.text.replace(datum["key"], datum["value"]) - elif "picture" == datum["type"]: - paragraph.clear() - paragraph.add_run().add_picture(datum["value"], width=Inches(datum["width"])) - elif "number" == datum["type"]: - paragraph.text = paragraph.text.replace(datum["key"], str(datum["value"])) - - # 表格替换 - for table in doc.tables: - for row in table.rows: - for cell in row.cells: - for datum in data: - if datum["key"] in cell.text: - if "text" == datum["type"]: - cell.text = cell.text.replace(datum["key"], datum["value"]) - elif "picture" == datum["type"]: - paragraph = cell.paragraphs[0] - paragraph.clear() - paragraph.add_run().add_picture(datum["value"], width=Inches(datum["width"])) - elif "number" == datum["type"]: - cell.text = cell.text.replace(datum["key"], str(datum["value"])) - doc.save(filename) - diff --git a/main.py b/main.py index be3b9da..9b66b8e 100644 --- a/main.py +++ b/main.py @@ -1,11 +1,26 @@ from docx import Document from docx.shared import Inches import sys +import json def get_command_argv_by_sys(): # 默认值-测试 template = "template.docx" filename = "demo.docx" + datafile = "" + number = len(sys.argv) + if 2 == number: + template = sys.argv[1] + if 3 == number: + template = sys.argv[1] + filename = sys.argv[2] + if 4 == number: + template = sys.argv[1] + filename = sys.argv[2] + datafile = sys.argv[3] + return (template, filename, datafile) + +def readData(datafile): data = [ {"key": "{Title}", "value": "测试标题", "type": "text"}, {"key": "{文件内容}", "value": "测试内容内容内容", "type": "text"}, @@ -16,17 +31,11 @@ def get_command_argv_by_sys(): {"key": "{Table.Col2}", "value": "列2", "type": "text"}, {"key": "{Table.Col3}", "value": "列3", "type": "text"}, ] - number = len(sys.argv) - if 1 == number: - template = sys.argv[0] - if 2 == number: - template = sys.argv[0] - filename = sys.argv[1] - if 3 == number: - template = sys.argv[0] - filename = sys.argv[1] - data = sys.argv[2] - return (template, filename, data) + if datafile != "": + with open(datafile, "r", encoding="utf-8") as file: + data = json.load(file) + return data + def replace(template, filename, data): docfile = Document(template) @@ -62,20 +71,8 @@ if __name__ == '__main__': params = get_command_argv_by_sys() template=params[0] filename=params[1] - data=params[2] - print(template) - print(filename) + datafile=params[2] + print(datafile) + data=readData(datafile) print(data) - template = "template.docx" - filename = "demo.docx" - data = [ - {"key": "{Title}", "value": "测试标题", "type": "text"}, - {"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"}, - ] replace(template, filename, data) diff --git a/readme.md b/readme.md index 9701e8a..dbd9ae4 100644 --- a/readme.md +++ b/readme.md @@ -1 +1,6 @@ -# 读取模板生成报表 \ No newline at end of file +# 读取模板生成报表 + + +```python +pyinstaller -F main.py -w +``` \ No newline at end of file diff --git a/setup.py b/setup.py deleted file mode 100644 index c21fdc5..0000000 --- a/setup.py +++ /dev/null @@ -1,15 +0,0 @@ -from setuptools import setup, Extension - -# 定义扩展模块 -extension = Extension( - name='add', - sources=['add.pyx'], # 指定 Cython 源文件 - language='c++' -) - -# 运行 setup -setup( - name='Add Function', - version='1.0', - ext_modules=[extension], -) \ No newline at end of file