您好,欢迎访问一九零五行业门户网

python通过openpyxl生成Excel文件的方法

本文实例讲述了python通过openpyxl生成excel文件的方法。分享给大家供大家参考。具体如下:
使用前请先安装openpyxl:
easy_install openpyxl
通过这个模块可以很方便的导出数据到excel
from openpyxl.workbook import workbookfrom openpyxl.writer.excel import excelwriterfrom openpyxl.cell import get_column_letterfrom openpyxl.style import color, fillfrom openpyxl.cell import cell#新建一个workbookwb = workbook()#第一个sheet是wsws = wb.worksheets[0]#设置ws的名称ws.title = u下单统计#给a1赋值ws.cell('a1').value = '%s'%(跟随总数)#给a2赋值#先把数字转换成字母col = get_column_letter(1)#赋值ws.cell('%s%s'%(col, 2)).value = '%s' % (a2“) #字体修改样式##颜色ws.cell('a2').style.font.color.index =color.green##字体名称ws.cell('a2').style.font.name ='arial'##字号ws.cell('a2').style.font.size =8##加粗ws.cell('a2').style.font.bold =true##不知道干啥用的ws.cell('a2').style.alignment.wrap_text =true##背景 好像不太好用 是个bugws.cell('a2').style.fill.fill_type =fill.fill_solidws.cell('a2').style.fill.start_color.index =color.darkred##修改某一列宽度ws.column_dimensions[c].width =60.0##增加一个表ws = wb.create_sheet()ws.title = u'结单统计'##保存生成xmlfile_name = 'test.xlsx'file_dir = '/home/x/'dest_filename = '%s%s'%(file_dir,file_name)ew = excelwriter(workbook = wb)ew = excelwriter(workbook = wb)
希望本文所述对大家的python程序设计有所帮助。
其它类似信息

推荐信息