如下所示:
# _*_ coding:utf-8 _*_ #----------------------------------------------- # import modules #----------------------------------------------- import os import xlwt import sys import types def set_style(name, height, bold = False): style = xlwt.XFStyle() #初始化樣式 font = xlwt.Font() #為樣式創(chuàng)建字體 font.name = name font.bold = bold font.color_index = 4 font.height = height style.font = font return style def write_excel(): #創(chuàng)建工作簿 workbook = xlwt.Workbook(encoding='utf-8') #創(chuàng)建sheet data_sheet = workbook.add_sheet('demo') #列表格式數(shù)據(jù) excelData = [ ['tdate', u'交易所', u'股票代碼'], [20170103, 'CNSESZ', '300319'], [20170104, 'CNSESZ', '300367'], [20170104, 'CNSESZ', '300367'] ] #定義循環(huán)下標(biāo) index = 0 for i in excelData: #每一列的內(nèi)容(i) for x, item inenumerate(i): #下標(biāo)(x),單元元素(item) data_sheet.write(index, x, item, set_style('Times New Roman',220, True)) index += 1 # sys.exit(); #保存文件 workbook.save('demo.xls') if __name__ =='__main__': write_excel() print ('創(chuàng)建demo.xlsx文件成功')