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.
82 lines
3.3 KiB
82 lines
3.3 KiB
from docx import Document
|
|
from docx.shared import Inches
|
|
import sys
|
|
|
|
def get_command_argv_by_sys():
|
|
# 默认值-测试
|
|
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"},
|
|
]
|
|
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)
|
|
|
|
def replace(template, filename, data):
|
|
docfile = Document(template)
|
|
# 段落替换
|
|
for paragraph in docfile.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 docfile.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"]))
|
|
docfile.save(filename)
|
|
|
|
if __name__ == '__main__':
|
|
params = get_command_argv_by_sys()
|
|
template=params[0]
|
|
filename=params[1]
|
|
data=params[2]
|
|
print(template)
|
|
print(filename)
|
|
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)
|