首页 > 编程 > Python > 正文

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

2020-02-23 01:11:43
字体:
来源:转载
供稿:网友

本文实例讲述了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程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表