excel export method for python data in list format

  • 2021-01-19 22:19:45
  • OfStack

This is as follows:


# _*_ coding:utf-8 _*_

#-----------------------------------------------
# import modules
#-----------------------------------------------
import os
import xlwt
import sys
import types
def set_style(name, height, bold = False):
style = xlwt.XFStyle() # Initialization style 
font = xlwt.Font() # Create a font for the style 
font.name = name
font.bold = bold
font.color_index = 4
font.height = height
style.font = font
return style
def write_excel():
# Creating a workbook 
workbook = xlwt.Workbook(encoding='utf-8')
# create sheet
data_sheet = workbook.add_sheet('demo')

# Tabular data 
excelData = [
['tdate', u' exchange ', u' Stock code '],
[20170103, 'CNSESZ', '300319'],
[20170104, 'CNSESZ', '300367'],
[20170104, 'CNSESZ', '300367']
]
# Define loop subscripts 
index = 0
for i in excelData:
# every 1 The content of the column (i)
for x, item inenumerate(i):
# The subscript (x) , unit element (item)
data_sheet.write(index, x, item, set_style('Times New Roman',220, True))
index += 1
# sys.exit();
# Save the file 
workbook.save('demo.xls')

if __name__ =='__main__':
write_excel()
print (' create demo.xlsx File successfully ')

Related articles: