Python使用xlwt模块 操作Excel文件
導(dǎo)出Excel文件
??? 1.?使用xlwt模塊 import xlwt
??????? import xlwt??? # 導(dǎo)入xlwt
? ? ? ? # 新建一個excel文件
file = xlwt.Workbook()?#注意這里的Workbook首字母是大寫,無語吧
? # 新建一個sheet
? table = file.add_sheet('sheet name')
??????? # 寫入數(shù)據(jù)table.write(行,列,value)
table.write(0,0,'test')
??????? # 如果對一個單元格重復(fù)操作,會引發(fā)
returns error: ? ?# Exception: Attempt to overwrite cell: # sheetname=u'sheet 1' rowx=0 colx=0? # 所以在打開時加cell_overwrite_ok=True?解決
? table = file.add_sheet('sheet name',cell_overwrite_ok=True?)
? file.save('demo.xls')? # 保存文件
?????? 另外,使用style
?????? style = xlwt.XFStyle()?#?初始化樣式
???? ? font = xlwt.Font()?#為樣式創(chuàng)建字體
???? ? font.name = 'Times New Roman'
?????? font.bold = True
?????? style.font = font?#為樣式設(shè)置字體
?????? table.write(0, 0, 'some bold Times text', style)?# 使用樣式
?????? xlwt 允許單元格或者整行地設(shè)置格式。還可以添加鏈接以及公式。可以閱讀源代碼,那里有例子:
?????? dates.py, 展示如何設(shè)置不同的數(shù)據(jù)格式
?????? hyperlinks.py, 展示如何創(chuàng)建超鏈接 (hint: you need to use a formula)
?????? merged.py, 展示如何合并格子
?????? row_styles.py, 展示如何應(yīng)用Style到整行格子中.
?
?例子一:
???????????? import xlwt
?wbk = xlwt.Workbook()
?sheet = wbk.add_sheet('sheet 1')
?# indexing is zero based, row then column
?sheet.write(0,1,'test text')
?sheet.write(1,1,'test text')
?wbk.save('test2.xls') ?默認(rèn)保存在桌面上
?
??????例子二:
import xlwt as ExcelWrite # 引入模塊
def _make_excel(self, data_array):if not data_array:return ''# data_array = sorted(data_array, key=lambda x:x['CreateTime']) # 按照數(shù)據(jù)的時間進(jìn)行排序# style_del = ExcelWrite.XFStyle()# style_del.alignment.wrap = 1 header = [u'下單日期',u'出發(fā)時間',u'出發(fā)時段',u'上車地點',u'下車地點',u'乘客名',u'司機信息',u'口岸',u'車隊',u'是否8座',u'支付渠道',u'支付金額',u'車隊報價',u'備注',u'備注2', u'訂單來源']xls = ExcelWrite.Workbook(style_compression=2) sheet = xls.add_sheet("Sheet1")sheet.col(3).width = (30*367) # 設(shè)置表格的寬度sheet.col(4).width = (30*367)sheet.col(5).width = (20*367)sheet.col(6).width = (20*367)sheet.col(11).width = (20*367)sheet.col(13).width = (20*367)i = 0# 寫表頭for each_header in header:sheet.write(0, i, each_header)i += 1row = 1
# 填充每行的數(shù)據(jù)for each_row in data_array:col = 0# 填充一行的每列數(shù)據(jù)for each_col in header:if each_col in (u'下單日期', u'出發(fā)時間'):
# self 對象表示類本身style = self._make_date_style() # 獲取樣式else:style = self._make_normal_style()if each_row['Status'] == 2:badBG = ExcelWrite.Pattern() # 設(shè)置背景badBG.pattern = badBG.SOLID_PATTERN# 灰色badBG.pattern_fore_colour = 23style.pattern = badBG# 刪除線style.font.struck_out = Truesheet.write(row, col, each_row[each_col], style)col += 1row += 1sf = StringIO.StringIO() # StringIO 此模塊可以學(xué)習(xí)下xls.save(sf) contents = sf.getvalue()sf.close()return contents
def _make_normal_style(self):style_normal = ExcelWrite.XFStyle() # 設(shè)置excel的樣式style_normal.alignment.wrap = 1return style_normaldef _make_date_style(self):style_date = ExcelWrite.XFStyle()style_date.alignment.wrap = 1style_date.num_format_str = u'mm月dd日'return style_date
# self 對象是繼承了tornado.web.RequestHandler的派生類
def _send_download(self, file_name, data):self.set_header('Content-Type', 'application/octet-stream')self.set_header('Content-Disposition', 'attachment; filename=' + file_name)self.write(data) # 將數(shù)據(jù)寫回到網(wǎng)頁客戶端self.finish()
# 調(diào)用生成excel數(shù)據(jù)
excel_data = self._make_excel(data) if excel_data:self._send_download('1.xls', excel_data) else:
pass
?
?
??????????????
?
轉(zhuǎn)載于:https://www.cnblogs.com/wind-wang/p/5663539.html
總結(jié)
以上是生活随笔為你收集整理的Python使用xlwt模块 操作Excel文件的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bash脚本基础
- 下一篇: iOS开发之功能模块--推送之坑问题解决