python-docx 取数据生成word并生成压缩包

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

获取板卡列表

card_id_list = str_inpara['card_id_list']
docx_list = []

循环每个板卡获取数据生成word

for card_id in card_id_list:
    op = MySqlShortConnectOperator()
    conn, Cursor = op.dbinit('mysql')

    sqlstr = "SELECT xxx where card_id=%d" % card_id
    loger.debug("sqlstr=%s", sqlstr)
    rst = do_query(sqlstr, Cursor)
    if rst["status"] != 0:
        loger.debug("select error [%s]", rst["errinfo"]);
        conn.commit()
        op.dbterm()
        return '{"status":"%s"result_list":[]}' % (rst["errinfo"])
    # out = {}
    hwinfo = rst["kvlist"]
    loger.debug("hwinfo=%s", hwinfo)
    # 本机xx
    sqlstr = "SELECT xxx  where card_id=%d" % card_id
    loger.debug("sqlstr=%s", sqlstr)
    rst = do_query(sqlstr, Cursor)
    if rst["status"] != 0:
        loger.debug("select error [%s]", rst["errinfo"]);
        conn.commit()
        op.dbterm()
        return '{"status":"%s"result_list":[]}' % (rst["errinfo"])
    # out = {}
    equ = rst["kvlist"]
    loger.debug("equ=%s", equ)
    # xxx变动
    sqlstr = "SELECT xxx  where card_id=%d" % card_id
    loger.debug("sqlstr=%s", sqlstr)
    rst = do_query(sqlstr, Cursor)
    if rst["status"] != 0:
        loger.debug("select error [%s]", rst["errinfo"]);
        conn.commit()
        op.dbterm()
        return '{"status":"%s"result_list":[]}' % (rst["errinfo"])
    bag = rst["kvlist"]
    #xx记录
    sqlstr = "SELECT xxx where card_id=%d" % card_id
    loger.debug("sqlstr=%s", sqlstr)
    rst = do_query(sqlstr, Cursor)
    if rst["status"] != 0:
        loger.debug("select error [%s]", rst["errinfo"]);
        conn.commit()
        op.dbterm()
        return '{"status":"%s"result_list":[]}' % (rst["errinfo"])
    overhaul = rst["kvlist"]
    # xx故障
    sqlstr = "SELECT XXX where card_id=%d" % card_id
    loger.debug("sqlstr=%s", sqlstr)
    rst = do_query(sqlstr, Cursor)
    if rst["status"] != 0:
        loger.debug("select error [%s]", rst["errinfo"]);
        conn.commit()
        op.dbterm()
        return '{"status":"%s"result_list":[]}' % (rst["errinfo"])
    major = rst["kvlist"]

    # xx
    sqlstr = "SELECT XXX where card_id=%d" % card_id
    loger.debug("sqlstr=%s", sqlstr)
    rst = do_query(sqlstr, Cursor)
    if rst["status"] != 0:
        loger.debug("select error [%s]", rst["errinfo"]);
        conn.commit()
        op.dbterm()
        return '{"status":"%s"result_list":[]}' % (rst["errinfo"])
    modified = rst["kvlist"]

    document = Document()

    paragraph = document.add_paragraph()
    run = paragraph.add_run(u'附表2:设备xxx')
    run.font.size=Pt(21)
    run.bold = True

    document.styles['Normal'].font.name = u'unknown'
    document.styles['Normal']._element.rPr.rFonts.set(qn('w:eastAsia'), u'unknown')



    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'\n\n设备名称  %s' %(equ[0]['equ_name']))
    #文字大小
    r1.font.size = Pt(21)
    #粗体
    r1.bold = True

    p2=document.add_paragraph()
    p2.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    p2.paragraph_format.line_spacing = Pt(25)
    r1 =p2.add_run(u'填 写 说 明')
    r1.font.size = Pt(11)
    r1.bold = True


    p3=document.add_paragraph()
    p3.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    p3.paragraph_format.line_spacing = Pt(20)
    r1 =p3.add_run(u'本簿xxxxxxxxxxxxxxxxx。')
    r1.font.size = Pt(11)
    p3=document.add_paragraph()
    p3.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    p3.paragraph_format.line_spacing = Pt(20)
    r1 =p3.add_run(u'xxxxxxxxxxxxxxx。')
    r1.font.size = Pt(11)
    p3=document.add_paragraph()
    p3.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    p3.paragraph_format.line_spacing = Pt(20)
    r1 =p3.add_run(u'本簿永久存档,随机调动。')
    r1.font.size = Pt(11)




    document.add_page_break()
    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'本机记录')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True
    table = document.add_table(rows=23,cols=6,style='Table Grid')

    table.cell(0,0).merge(table.cell(2,2))
    table.cell(0,3).merge(table.cell(2,5))
    table.cell(3,0).merge(table.cell(5,1))
    table.cell(3,2).merge(table.cell(5,3))
    table.cell(3,4).merge(table.cell(5,5))
    table.cell(6,0).merge(table.cell(6,5))
    table.cell(7,0).merge(table.cell(7,1))
    table.cell(7,2).merge(table.cell(7,3))
    table.cell(7,4).merge(table.cell(7,5))
    table.cell(8,0).merge(table.cell(10,5))
    table.cell(11,0).merge(table.cell(13,5))
    table.cell(14,0).merge(table.cell(16,5))
    table.cell(17,0).merge(table.cell(19,5))
    table.cell(20,0).merge(table.cell(22,1))
    table.cell(20,2).merge(table.cell(22,3))
    table.cell(20,4).merge(table.cell(22,5))

    capability = equ[0]['capability'] if None else ''
    hdr_cells0 = table.rows[0].cells
    hdr_cells0[0].add_paragraph(str('型号:%s' %(equ[0]['equ_model'])).decode('utf-8'))
    hdr_cells0[0].add_paragraph(str('容量:%s' %(capability)).decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('名称:%s' %(equ[0]['equ_name'])).decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('规格:').decode('utf-8'))
    hdr_cells1 = table.rows[3].cells
    hdr_cells1[0].add_paragraph(str('xx厂家: %s' %(equ[0]['equ_vendor'])).decode('utf-8'))
    hdr_cells1[0].add_paragraph(str('联系电话:').decode('utf-8'))
    hdr_cells1[2].add_paragraph(str('出厂日期:').decode('utf-8'))
    hdr_cells1[4].add_paragraph(str('出厂序号:').decode('utf-8'))
    hdr_cells4 = table.rows[6].cells
    hdr_cells4[0].add_paragraph(str('xxxx地点: %s' %(equ[0]['first_installation_site'])).decode('utf-8'))
    hdr_cells5 = table.rows[7].cells
    hdr_cells5[0].add_paragraph(str('xx装机: %s' %(equ[0]['first_installation_time'])).decode('utf-8'))
    hdr_cells5[2].add_paragraph(str('xx编号: %s' %(equ[0]['equ_number'])).decode('utf-8'))
    hdr_cells5[4].add_paragraph(str('xx: %s' %(equ[0]['equ_value'])).decode('utf-8'))
    hdr_cells6 = table.rows[8].cells
    hdr_cells6[0].add_paragraph(str('xxxxxx及份数: %s' %(equ[0]['random_explain'])).decode('utf-8'))
    hdr_cells10 = table.rows[11].cells
    hdr_cells10[0].add_paragraph(str('xxxxx及数量: %s' %(equ[0]['random_tool'])).decode('utf-8'))
    hdr_cells13 = table.rows[14].cells
    hdr_cells13[0].add_paragraph(str('xxxxx及数量: %s' %(equ[0]['random_spare'])).decode('utf-8'))
    hdr_cells16 = table.rows[17].cells
    hdr_cells16[0].add_paragraph(str('xxx及说明: %s' %(equ[0]['premark'])).decode('utf-8'))
    hdr_cells19 = table.rows[20].cells
    hdr_cells19[0].add_paragraph(str('本表填写日期:').decode('utf-8'))
    hdr_cells19[2].add_paragraph(str('填表人:').decode('utf-8'))
    hdr_cells19[4].add_paragraph(str('填制单位盖章:').decode('utf-8'))

    document.add_page_break()
    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'xxxx记录')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True
    a = len(hwinfo)+1
    if a < 2:
        a = 21
    table2 = document.add_table(rows=a,cols=6,style='Table Grid')
    hdr_cells0 = table2.rows[0].cells
    hdr_cells0[0].add_paragraph(str('xxxx(站)名').decode('utf-8'))
    hdr_cells0[1].add_paragraph(str('开始年月').decode('utf-8'))
    hdr_cells0[2].add_paragraph(str('终止年月').decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('调动文件号').decode('utf-8'))
    hdr_cells0[4].add_paragraph(str('经办人').decode('utf-8'))
    hdr_cells0[5].add_paragraph(str('备     注').decode('utf-8'))

    for i in range(0, len(hwinfo)):
        j = i + 1
        hdr_cells0 = table2.rows[j].cells
        print j
        hdr_cells0[0].add_paragraph(str(hwinfo[i]['installed_station']).decode('utf-8'))
        hdr_cells0[1].add_paragraph(str(hwinfo[i]['end_date']).decode('utf-8'))
        hdr_cells0[2].add_paragraph(str(hwinfo[i]['premark']).decode('utf-8'))
        hdr_cells0[3].add_paragraph(str(hwinfo[i]['transfer_file_number']).decode('utf-8'))
        hdr_cells0[4].add_paragraph(str(hwinfo[i]['operator_name']).decode('utf-8'))
        hdr_cells0[5].add_paragraph(str(hwinfo[i]['start_date']).decode('utf-8'))
    document.add_page_break()
    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'xxx变动记录')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True
    a = len(bag) + 1
    if a < 2:
        a = 21
    table3 = document.add_table(rows=a,cols=5,style='Table Grid')
    hdr_cells0 = table3.rows[0].cells
    hdr_cells0[0].add_paragraph(str('xxx)名').decode('utf-8'))
    hdr_cells0[1].add_paragraph(str('xxx  人').decode('utf-8'))
    hdr_cells0[2].add_paragraph(str('开 始 年 月').decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('终 止 年 月').decode('utf-8'))
    hdr_cells0[4].add_paragraph(str('备    注').decode('utf-8'))
    for i in range(0, len(bag)):
        j = i + 1
        hdr_cells0 = table3.rows[j].cells
        print j
        hdr_cells0[0].add_paragraph(str(bag[i]['installed_station']).decode('utf-8'))
        hdr_cells0[1].add_paragraph(str(bag[i]['bag_name']).decode('utf-8'))
        hdr_cells0[2].add_paragraph(str(bag[i]['start_date']).decode('utf-8'))
        hdr_cells0[3].add_paragraph(str(bag[i]['end_date']).decode('utf-8'))
        hdr_cells0[4].add_paragraph(str(bag[i]['premark']).decode('utf-8'))



    document.add_page_break()
    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'xxxxxx')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True
    a = len(overhaul)+1
    if a < 2:
        a = 21
    table4 = document.add_table(rows=a,cols=5,style='Table Grid')
    hdr_cells0 = table4.rows[0].cells
    hdr_cells0[0].add_paragraph(str('xx项目').decode('utf-8'))
    hdr_cells0[1].add_paragraph(str('xx值').decode('utf-8'))
    hdr_cells0[2].add_paragraph(str('xx值').decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('xx者').decode('utf-8'))
    hdr_cells0[4].add_paragraph(str('时间').decode('utf-8'))
    for i in range(0, len(overhaul)):
        j = i + 1
        hdr_cells0 = table4.rows[j].cells
        print j
        hdr_cells0[0].add_paragraph(str(overhaul[i]['test_project']).decode('utf-8'))
        hdr_cells0[1].add_paragraph(str(overhaul[i]['spec']).decode('utf-8'))
        hdr_cells0[2].add_paragraph(str(overhaul[i]['test_value']).decode('utf-8'))
        hdr_cells0[3].add_paragraph(str(overhaul[i]['test_name']).decode('utf-8'))
        hdr_cells0[4].add_paragraph(str(overhaul[i]['date_time']).decode('utf-8'))


    document.add_page_break()
    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'主要xxxxx')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True

    a = len(major)+1
    if a < 2:
        a = 25
    table5 = document.add_table(rows=a,cols=4,style='Table Grid')

    hdr_cells0 = table5.rows[0].cells
    hdr_cells0[0].add_paragraph(str('年  月  日').decode('utf-8'))
    hdr_cells0[1].add_paragraph(str('xxxx及情况').decode('utf-8'))
    hdr_cells0[2].add_paragraph(str('xx处 理 经 过').decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('xx者').decode('utf-8'))

    for i in range(0, len(major)):
        j = i + 1
        # hdr_cells0 = table4.rows[j].cells
        hdr_cells0 = table5.rows[j].cells
        hdr_cells0[0].add_paragraph(str(major[i]['serviving_time']).decode('utf-8'))
        hdr_cells0[1].add_paragraph(str(major[i]['fault_condition']).decode('utf-8'))
      hdr_cells0[2].add_paragraph(str(major[i['treating_processes']).decode('utf8'))
        hdr_cells0[3].add_paragraph(str(major[i]['operator_name']).decode('utf-8'))


    document.add_page_break()

    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'xx经历')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True
    a = len(modified)+1
    if a < 2:
        a = 26
    table6 = document.add_table(rows=a,cols=7,style='Table Grid')
    hdr_cells0 = table6.rows[0].cells
    hdr_cells0[0].add_paragraph(str('年').decode('utf-8'))
    hdr_cells0[1].add_paragraph(str('月').decode('utf-8'))
    hdr_cells0[2].add_paragraph(str('日').decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('xx部分').decode('utf-8'))
    hdr_cells0[4].add_paragraph(str('xx原因、变动情况和性能').decode('utf-8'))
    hdr_cells0[5].add_paragraph(str('相关xx、图纸').decode('utf-8'))
    hdr_cells0[6].add_paragraph(str('xx人').decode('utf-8'))
    for i in range(0, len(modified)):
        j = i + 1
        hdr_cells0 = table6.rows[j].cells
        print j
        hdr_cells0[0].add_paragraph(str(modified[i]['year']).decode('utf-8'))
        hdr_cells0[1].add_paragraph(str(modified[i]['month']).decode('utf-8'))
        hdr_cells0[2].add_paragraph(str(modified[i]['day']).decode('utf-8'))
        hdr_cells0[3].add_paragraph(str(modified[i]['refit_part']).decode('utf-8'))
        hdr_cells0[4].add_paragraph(str(modified[i]['refit_details']).decode('utf-8'))
        hdr_cells0[5].add_paragraph(str(modified[i]['filename']).decode('utf-8'))
        hdr_cells0[6].add_paragraph(str(modified[i]['regit_people']).decode('utf-8'))
    document.add_page_break()

    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'xx附图')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True
    table7 = document.add_table(rows=36,cols=7,style='Table Grid')
    table7.cell(0,0).merge(table7.cell(35,6))

    document.add_page_break()
    p1=document.add_paragraph()
    #设置p1段落居中
    p1.paragraph_format.alignment = WD_ALIGN_PARAGRAPH.CENTER
    #p1段落大小
    p1.paragraph_format.line_spacing = Pt(25)
    #给p1段落添加run对象r1,并设置文本
    r1 =p1.add_run(u'客户端xxxx表')
    #文字大小
    r1.font.size = Pt(16)
    #粗体
    r1.bold = True
    table8 = document.add_table(rows=7,cols=10,style='Table Grid')
    table8.cell(1,0).merge(table8.cell(3,0))
    table8.cell(1,1).merge(table8.cell(3,1))
    table8.cell(1,2).merge(table8.cell(3,2))
    table8.cell(1,3).merge(table8.cell(3,3))
    table8.cell(1,4).merge(table8.cell(3,4))
    table8.cell(1,5).merge(table8.cell(3,5))
    table8.cell(1,6).merge(table8.cell(3,6))
    table8.cell(1,7).merge(table8.cell(3,7))
    table8.cell(1,8).merge(table8.cell(3,8))
    table8.cell(1,9).merge(table8.cell(3,9))

    table8.cell(4,0).merge(table8.cell(6,0))
    table8.cell(4,1).merge(table8.cell(6,1))
    table8.cell(4,2).merge(table8.cell(6,2))
    table8.cell(4,3).merge(table8.cell(6,3))
    table8.cell(4,4).merge(table8.cell(6,4))
    table8.cell(4,5).merge(table8.cell(6,5))
    table8.cell(4,6).merge(table8.cell(6,6))
    table8.cell(4,7).merge(table8.cell(6,7))
    table8.cell(4,8).merge(table8.cell(6,8))
    table8.cell(4,9).merge(table8.cell(6,9))

    hdr_cells0 = table8.rows[0].cells
    hdr_cells0[0].add_paragraph(str('序号').decode('utf-8'))
    hdr_cells0[1].add_paragraph(str('xx类别').decode('utf-8'))
    hdr_cells0[2].add_paragraph(str('xx型号').decode('utf-8'))
    hdr_cells0[3].add_paragraph(str('xx局所').decode('utf-8'))
    hdr_cells0[4].add_paragraph(str('xx标识').decode('utf-8'))
    hdr_cells0[5].add_paragraph(str('xx归属').decode('utf-8'))
    hdr_cells0[6].add_paragraph(str('xx单位').decode('utf-8'))
    hdr_cells0[7].add_paragraph(str('xx界面').decode('utf-8'))
    hdr_cells0[8].add_paragraph(str('安装地点').decode('utf-8'))
    hdr_cells0[9].add_paragraph(str('安装时间').decode('utf-8'))



    document.save(r'./htdocs/computercalendar/%s.docx' % equ[0]['equ_name'])
    docx_list.append(r'./htdocs/computercalendar/%s.docx' % equ[0]['equ_name'])


azip = zipfile.ZipFile(r'./htdocs/computercalendar/tmp.zip', 'w')
for i in docx_list:
    azip.write(i.encode('GBK'))
azip.close()

你可能感兴趣的:(python-docx 取数据生成word并生成压缩包)