python生成docx文件

使用python自动生成一张想要的docx文件
在这其中有指纹和公司盖章

from PIL import Image
from docx import Document
from docx.oxml.ns import qn
from docx.shared import Pt, Inches, Cm
from docx.enum.text import WD_PARAGRAPH_ALIGNMENT
from xlsxtpl.writerx import BookWriter

def work_experience_word_file(user_info, zip_file_path, middle_name=""):
    """
    创建word文档
    @param user_info: 用户的基本信息
    @param zip_file_path: 压缩包地址信息
    @param middle_name: 为了满足成员附件导出的文件名
    """
    # 创建代表Word文档的Doc对象
    doc = Document()
    # 保存文档的地址
    doc.styles['Normal'].font.name = u'仿宋'
    doc.styles['Normal'].font.size = Pt(16)
    doc.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'仿宋')

    # 标题
    title_name = '个人从业经历承诺书'
    title_paragraph = doc.add_paragraph()
    title_paragraph.alignment = WD_PARAGRAPH_ALIGNMENT.CENTER  # 居中对齐
    title_run = title_paragraph.add_run(title_name)
    title_run.bold = True  # 加粗
    title_run.font.size = Pt(22)

    # 换行
    wrap_str = ''
    doc.add_paragraph(wrap_str)

    # 身份信息的填充
    info_one = '本人'
    info_one_paragraph = doc.add_paragraph(info_one)
    user_name = info_one_paragraph.add_run(f' {user_info["name"]} ')
    user_name.underline = True  # 下划线

    info_one_paragraph.add_run('于')

    p_format = info_one_paragraph.paragraph_format
    p_format.first_line_indent = Inches(0.4)  # 首行缩进
    p_format.line_spacing = 1.5  # 设置行间距为 1.5

    work_start_time = user_info.get("work_start_time").split('-') if user_info.get("work_start_time") else ['', '']
    work_start_year = info_one_paragraph.add_run(f' {work_start_time[0]} ')
    work_start_year.underline = True  # 下划线

    info_two = '年'
    info_one_paragraph.add_run(info_two)

    work_start_month = info_one_paragraph.add_run(f' {work_start_time[1]} ')
    work_start_month.underline = True  # 下划线

    info_three = '月工作至今,现已从事'
    info_one_paragraph.add_run(info_three)

    work_place = info_one_paragraph.add_run(
        f' {user_info.get("operitem_str") if user_info.get("operitem_str", "") else user_info.get("job_class_str", "")} ')
    work_place.underline = True  # 下划线

    info_one_paragraph.add_run('相关工作')

    work_year = info_one_paragraph.add_run(f' {user_info.get("work_year", "")} ')
    work_year.underline = True  # 下划线

    info_one_paragraph.add_run('年。')

    # 换行
    wrap_str = ''
    doc.add_paragraph(wrap_str)
    # 换行
    wrap_str = ''
    doc.add_paragraph(wrap_str)

    # 培训单位公章
    info_five = doc.add_paragraph("承诺人:   ( 签名并盖手印 )")
    info_five_format = info_five.paragraph_format
    info_five_format.first_line_indent = Inches(0.8)  # 首行缩进
    p_format.line_spacing = 1.5  # 设置行间距为 1.5
    info_five.alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT

    # 签名和指纹的地址
    zw_seal_url = zip_file_path + '/' + user_info['name'] + '指纹文件'
    zw_path = zw_seal_url + '.png'
    if os.path.exists(zw_path):
        add_float_picture(info_five, zw_path, width=Cm(2.0), pos_x=Cm(15.1), pos_y=Cm(9.6))
    else:
        zw_path = zw_seal_url + '.jpg'
        if os.path.exists(zw_path):
            add_float_picture(info_five, zw_path, width=Cm(2.0), pos_x=Cm(15.1), pos_y=Cm(9.6))

    qm_seal_url = zip_file_path + '/' + user_info['name'] + '签名文件'
    qm_path = qm_seal_url + '.png'
    if os.path.exists(qm_path):
        add_float_picture(info_five, qm_path, width=Cm(3.0), pos_x=Cm(14.6), pos_y=Cm(9.6))
    else:
        qm_path = qm_seal_url + '.jpg'
        if os.path.exists(zw_path):
            add_float_picture(info_five, qm_path, width=Cm(3.0), pos_x=Cm(14.6), pos_y=Cm(9.6))

    # 换行
    wrap_str = ''
    doc.add_paragraph(wrap_str)

    # 盖章日期
    create_time_list = user_info['createTime'].split('-') if user_info['createTime'] else "   "
    create_time = f'{create_time_list[0]}{create_time_list[1]}{create_time_list[2]}日'
    doc.add_paragraph(create_time).alignment = WD_PARAGRAPH_ALIGNMENT.RIGHT  # 右对齐

    word_file_path = zip_file_path + f'/{middle_name}{user_info["id_card"]}.docx'


user_info = {"createTime": "", ...........}
zip_file_path = 'E:/'
work_experience_word_file(user_info, zip_file_path, middle_name="")

你可能感兴趣的:(python,python,c#,microsoft)