3.3 通过模板生成文件

from docx import Document
from datetime import datetime
import os

date = datetime.now()
infos = [
    ['浙ADZZZZA', date.year, date.month, date.day, date.hour, date.minute, '违停', 200],
    ['浙ADZZZZB', date.year, date.month, date.day, date.hour, date.minute, '闯红灯', 300],
    ['浙ADZZZZC', date.year, date.month, date.day, date.hour, date.minute, '未礼让行人', 150],
    ['浙ADZZZZD', date.year, date.month, date.day, date.hour, date.minute, '酒驾', 600]
]

for info in infos:
    document = Document('./办公自动化/files/违章通知书-模板.docx')
    for p in document.paragraphs:
        # print(p.text)
        # p.text = p.text.replace('{0}', info[0])
        # p.text = p.text.replace('{1}', str(info[1]))
        # p.text = p.text.replace('{2}', str(info[2]))
        # p.text = p.text.replace('{3}', str(info[3]))
        # p.text = p.text.replace('{4}', str(info[4]))
        # p.text = p.text.replace('{5}', str(info[5]))
        # p.text = p.text.replace('{6}', info[6])
        # p.text = p.text.replace('{7}', str(info[7]))
        for run in p.runs:   # 保留原文档样式
            run.text = run.text.replace('{0}', info[0])
            run.text = run.text.replace('{1}', str(info[1]))
            run.text = run.text.replace('{2}', str(info[2]))
            run.text = run.text.replace('{3}', str(info[3]))
            run.text = run.text.replace('{4}', str(info[4]))
            run.text = run.text.replace('{5}', str(info[5]))
            run.text = run.text.replace('{6}', info[6])
            run.text = run.text.replace('{7}', str(info[7]))

    if not os.path.exists(f'./办公自动化/files/违章通知书-{info[0]}.docx'):
        document.save(f'./办公自动化/files/违章通知书-{info[0]}.docx')