zoukankan      html  css  js  c++  java
  • xlwt以格式生成xls文件

    参考: http://blog.sina.com.cn/s/blog_5357c0af01019gjo.html
    http://www.programcreek.com/python/example/56293/xlwt.easyxf

    import xlrd, xlwt
    import re
    import os
    
    
    wbs = {}
    SPLIT_NUM = 6
    FN = 'yeah.xls'
    SN = ''
    
    header_style = xlwt.easyxf('font:bold on, height 280;align: horz center; border: top thin, right thin, bottom thin, left thin;')
    header_height = xlwt.easyxf('font:height 500;')
    
    title_style = xlwt.easyxf('font:bold on, height 220;border: top thin, right thin, bottom thin, left thin;')
    title_height = xlwt.easyxf('font:height 500;')
    
    cell_style = xlwt.easyxf('font:height 200;border: top thin, right thin, bottom thin, left thin;')
    cell_height = xlwt.easyxf('font:height 400;')
    
    CHARW = 650       
    
    with xlrd.open_workbook(FN) as book:
        if SN:
            sht = book.sheet_by_name(SN)
        else:
            sht = book.sheets()[0]
        columns = [e.value for e in sht.row(0)]
        for r in range(1, sht.nrows):
            row = [e.value for e in sht.row(r)]
            name = row[SPLIT_NUM]
            sfz = row[1]
            xm = row[2]
            yh = row[7]
            jtdm = row[3]
            if name not in wbs:
                wb = xlwt.Workbook()
                ws = wb.add_sheet(name)
                wbs[name] = {'wb':wb, 'ws':ws, 'r':4}
                ws.write_merge(0, 0, 0, 4, '领卡签收表清单', header_style)
                ws.write_merge(1, 1, 0, 1, '银行网点名称', header_style)
                ws.write_merge(1, 1, 2, 4, yh, header_style)
                ws.write_merge(2, 2, 0, 1, '集体名称', header_style)
                ws.write_merge(2, 2, 2, 4, name, header_style)
                ws.write_merge(3, 3, 0, 1, '集体代码', header_style)
                ws.write_merge(3, 3, 2, 4, jtdm, header_style)
                for r in range(4):
                    ws.row(r).set_style(header_height)
                for col, e  in enumerate([
                    ('序号', CHARW*3), ('姓名', CHARW*5),
                    ('身份证号码', CHARW*11),('领卡人联系电话', CHARW*8),
                    ('领卡人签名', CHARW*6)]):
                    label = e[0]
                    width = e[1]
                    ws.write(4, col, label, title_style)
                    ws.col(col).width = width
                ws.row(4).set_style(title_height)      
            else:
                ws = wbs[name]['ws']
            wbs[name]['r'] += 1
            cr = wbs[name]['r']
            ws.write(cr, 0, cr-4, cell_style)
            ws.write(cr, 1, xm, cell_style)
            ws.write(cr, 2, sfz, cell_style)
            ws.write(cr, 3, '', cell_style)
            ws.write(cr, 4, '', cell_style)
            ws.row(cr).set_style(cell_height)
        for k, v in wbs.items():
            v['wb'].save(k+'.xls')
            print('成功:'+k+'.xls')
    
    
  • 相关阅读:
    动态规划
    Python第二天学习
    Python第一天学习---基础语法
    java易错知识点
    C语言---指针复习
    排序汇总
    课程设计---创建族谱管理系统
    Vue第五篇 Vue的生命周期
    Vue第四篇 Vue路由系统
    Vue第三篇 Vue组件
  • 原文地址:https://www.cnblogs.com/xiangnan/p/6841824.html
Copyright © 2011-2022 走看看