python通过openpyxl生成Excel文件,pythonopenpyxl,使用前请先安装openp
python通过openpyxl生成Excel文件,pythonopenpyxl,使用前请先安装openp
使用前请先安装openpyxl:easy_installopenpyxl通过这个模块可以很方便的导出数据到Excel```pythonfrom openpyxl.workbook import Workbookfrom openpyxl.writer.excel import ExcelWriterfrom openpyxl.cell import get_column_letterfrom openpyxl.style import Color, Fillfrom openpyxl.cell import Cell
新建一个workbook
wb = Workbook()
第一个sheet是ws
ws = 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
背景 好像不太好用 是个BUG
ws.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'结单统计'
保存生成xml
file_name = 'test.xlsx'file_dir = '/home/x/'dest_filename = '%s%s'%(file_dir,file_name)
ew = ExcelWriter(workbook = wb)
ew = ExcelWriter(workbook = wb)```
相关内容
- python通过pil生成图片验证码,,# -*- coding
- python操作sqlite的CRUD,pythonsqlitecrud,import sqlit
- python操作sqlite的CRUD,pythonsqlitecrud,import sqlit
- python开发的简单窗口界面的倒计时界面,python倒计时
- 用while编代码计算1+2+……+100,while,a=1b=2n=1whi
- 网站搬运工,,# -*- encodi
- python获取当天日期,python获取当天,import datet
- python编程从一个ftp传输文件到另一个ftp服务器,python传
- python提交post数据到远程url,python提交posturl,import sys,
- python从ftp服务器下载文件,pythonftp下载文件,import ftpl
评论关闭