如下所示:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
|
# _*_ 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文件成功' )
|
以上这篇python list格式数据excel导出方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/ren_273086429/article/details/79072553