# _*_ coding:utf-8 _*_#-----------------------------------------------# import modules#-----------------------------------------------import osimport xlwtimport sysimport typesdef set_style(name, height, bold = False):style = xlwt.XFStyle() #初始化样式font = xlwt.Font() #为样式创建字体font.name = namefont.bold = boldfont.color_index = 4font.height = heightstyle.font = fontreturn styledef write_excel():#创建工作簿workbook = xlwt.Workbook(encoding='utf-8')#创建sheetdata_sheet = workbook.add_sheet('demo')#列表格式数据excelData = [['tdate', u'交易所', u'股票代码'],[20170103, 'CNSESZ', '300319'],[20170104, 'CNSESZ', '300367'],[20170104, 'CNSESZ', '300367']]#定义循环下标index = 0for i in excelData:#每一列的内容(i)for x, item inenumerate(i):#下标(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 ('创建demo.xlsx文件成功')