3.2 案例-违章通知书

from docx import Document
from docx.shared import Pt, RGBColor, Inches
from docx.oxml.ns import qn
from docx.enum.text import WD_ALIGN_PARAGRAPH

# 违章通知书
def create_doc(car_num, year, month, day, hour, minute, act, money):
    doc = Document()
    title = doc.add_heading()

    run = title.add_run('车辆违章处罚通知书', 0)

    run.font.size = Pt(30)
    run.font.bold = True
    run.font.color.rgb = RGBColor(255, 0, 0)
    run.font.name = ''
    run._element.rPr.rFonts.set(qn('w:eastAsia'), '黑体')

    title.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER

    info = f'''经查,你驾驶牌号为{car_num}的小型轮式拖拉机于{year}年{month}月{day}日{hour}时{minute}分许,因{act}违章行为,被交通技术监控设备记录;罚款{money}元,请及时接受处理。
            '''

    content = doc.add_paragraph()
    run2 = content.add_run(info)
    content.paragraph_format.first_line_indent = Inches(0.5)
    run2.font.size = Pt(18)
    run2.font.bold = True
    run2.font.name = ''
    run2._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')

    content.paragraph_format.space_before = Pt(80)

    content.paragraph_format.line_spacing = 2

    end = doc.add_paragraph()
    end_info = "驾驶员签字:                    时间:     "
    end.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.RIGHT
    run3 = end.add_run(end_info)
    end.paragraph_format.first_line_indent = Inches(0.5)
    end.paragraph_format.right_indent = Inches(0.6)
    run3.font.size = Pt(18)
    run3.font.name = ''
    run3._element.rPr.rFonts.set(qn('w:eastAsia'), '宋体')

    content.paragraph_format.space_before = Pt(50)

    content.paragraph_format.line_spacing = 2

    doc.save('./办公自动化/files/违章通知书.docx')

if __name__ == '__main__':
    from datetime import datetime
    date = datetime.now()
    create_doc('浙ADZZZZZ', date.year, date.month, date.day, date.hour, date.minute, '违停', 200)