时间:2020-11-29 11:04:12 | 栏目:Python代码 | 点击:次
如下所示:
# _*_ 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() #为样式创建字体 font.name = name font.bold = bold font.color_index = 4 font.height = height style.font = font return style def write_excel(): #创建工作簿 workbook = xlwt.Workbook(encoding='utf-8') #创建sheet data_sheet = workbook.add_sheet('demo') #列表格式数据 excelData = [ ['tdate', u'交易所', u'股票代码'], [20170103, 'CNSESZ', '300319'], [20170104, 'CNSESZ', '300367'], [20170104, 'CNSESZ', '300367'] ] #定义循环下标 index = 0 for 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文件成功')