You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
84 lines
2.6 KiB
84 lines
2.6 KiB
import json
|
|
import sys
|
|
|
|
from docx import Document
|
|
from docx.shared import Inches
|
|
|
|
|
|
def get_command_argv_by_sys():
|
|
# 默认值-用于测试
|
|
template_path = "template.docx"
|
|
filename_path = "demo.docx"
|
|
datafile_path = "data.json"
|
|
# 读取参数
|
|
number = len(sys.argv)
|
|
if 2 == number:
|
|
template_path = sys.argv[1]
|
|
if 3 == number:
|
|
template_path = sys.argv[1]
|
|
filename_path = sys.argv[2]
|
|
if 4 == number:
|
|
template_path = sys.argv[1]
|
|
filename_path = sys.argv[2]
|
|
datafile_path = sys.argv[3]
|
|
return template_path, filename_path, datafile_path
|
|
|
|
|
|
def read_data(filepath):
|
|
content = []
|
|
if filepath != "":
|
|
try:
|
|
with open(filepath, "r", encoding="utf-8") as file:
|
|
content = json.load(file)
|
|
finally:
|
|
return content
|
|
|
|
|
|
def replace(template_path, filename_path, data_json):
|
|
try:
|
|
document_file = Document(template_path)
|
|
except:
|
|
return 2
|
|
# 段落替换
|
|
for paragraph in document_file.paragraphs:
|
|
for datum in data_json:
|
|
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 document_file.tables:
|
|
for row in table.rows:
|
|
for cell in row.cells:
|
|
for datum in data_json:
|
|
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"]))
|
|
document_file.save(filename_path)
|
|
return 0
|
|
|
|
|
|
def main():
|
|
params = get_command_argv_by_sys()
|
|
[template, filename, datafile] = params
|
|
data = read_data(datafile)
|
|
if len(data) == 0:
|
|
return 1
|
|
else:
|
|
return replace(template, filename, data)
|
|
|
|
|
|
if __name__ == '__main__':
|
|
print(main())
|