python导出excel
import os
import xlwt
if __name__ == '__main__':
ws = xlwt.Workbook(encoding='utf8')
worksheet = ws.add_sheet('今天学习python导出excel')
data = [{
'name':'xxx','age':12,'address':'北京'
}]
worksheet.write(0,0,'姓名')
worksheet.write(0,1,'年龄')
worksheet.write(0,2,'地址')
excel_row = 1
for i in data:
worksheet.write(excel_row,0,i['name'])
worksheet.write(excel_row,1,i['age'])
worksheet.write(excel_row,2,i['address'])
excel_row += 1
exist_file = os.path.exists('今天学习python.xls')
if exist_file:
os.remove(r'今天学习python.xls')
ws.save('今天学习python.xls')
os.startfile(r'C:\Users\小董\Desktop\python 导出excel\今天学习python.xls')
|