本文實(shí)例講述了python通過openpyxl生成Excel文件的方法。分享給大家供大家參考。具體如下:
使用前請(qǐng)先安裝openpyxl:
easy_install openpyxl
通過這個(gè)模塊可以很方便的導(dǎo)出數(shù)據(jù)到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#新建一個(gè)workbookwb = Workbook()#第一個(gè)sheet是wsws = wb.worksheets[0]#設(shè)置ws的名稱ws.title = u"下單統(tǒng)計(jì)"#給A1賦值ws.cell('A1').value = '%s'%("跟隨總數(shù)")#給A2賦值#先把數(shù)字轉(zhuǎn)換成字母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'##字號(hào)ws.cell('A2').style.font.size =8##加粗ws.cell('A2').style.font.bold =True##不知道干啥用的ws.cell('A2').style.alignment.wrap_text =True##背景 好像不太好用 是個(gè)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##增加一個(gè)表ws = wb.create_sheet()ws.title = u'結(jié)單統(tǒng)計(jì)'##保存生成xmlfile_name = 'test.xlsx'file_dir = '/home/x/'dest_filename = '%s%s'%(file_dir,file_name)ew = ExcelWriter(workbook = wb)ew = ExcelWriter(workbook = wb)希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選