首页 > 技术文章 > xlwt模块的使用

bqwzx 2019-04-05 11:43 原文

前记:Python处理表格时会用到xlwt和xlrd模块

xlwt设置行高:row

sheet.row(2).set_style(xlwt.easyxf('font:height 440;'))
13.5(220)是默认行高,13.5=220,27=440

xlwt设置列宽:col

sheet.col(2).width = 256*8
8.43(2343,256*9.15)是默认列宽,8.29=256*9,10.29=256*11

xlwt设置字体

style = xlwt.easyxf("""
                font: 
                    height 220, 
                    name SimSun, 
                    colour_index black, 
                    bold off; 
                align: 
                    wrap on, 
                    vert centre, 
                    horiz center;
                borders:
                    left thin, 
                    right thin, 
                    top thin, 
                    bottom thin
                 """)
sheet.write_merge(0, 0, 0, 21, '哈哈哈', style) 
sheet.write(2, 3, '哈哈哈', style)    

xlwt导出表的开头和结尾

response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = u'attachment;filename=dami.xls'
wb = xlwt.Workbook(encoding='utf8')
sheet = wb.add_sheet(u'商品库存', cell_overwrite_ok=True)

output = BytesIO()
wb.save(output)
output.seek(0)
response.write(output.getvalue())
return response

写表格方法

sheet.write(行,列,标签,样式)
sheet.write_merge(上,下,左,右,标签,样式)

另外常用样式

xlwt模块:
style = xlwt.easyxf("""
            font:
                name Arial,
                colour_index white,
                bold on,
                height 0xA0;
            align:
                wrap off,
                vert center,
                horiz center;
            pattern:
                pattern solid,
                fore-colour 0x19;
            borders:
                left thin,
                right thin,
                top thin,
                bottom thin;
            """)
style2 = xlwt.easyxf("""
                font: 
                    height 220, 
                    name SimSun, 
                    colour_index black, 
                    bold off; 
                align: 
                    wrap on, 
                    vert centre, 
                    horiz center;
                borders:
                    left thin, 
                    right thin, 
                    top thin, 
                    bottom thin
                 """)
写入时设置样式改变字体属性,改变边框属性,改变底色属性,改变行高(起码显示上改变),但没改变行宽。
行高行宽可以自行设置。

推荐阅读