❶ python怎麼把數據輸出到excel
python導出數據到excel文件的方法:
1、調用Workbook()對象中的add_sheet()方法
1
2
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
2、通過add_sheet()方法中的write()函數將數據寫入到excel中,然後使用save()函數保存excel文件
1
2
3
4
5
6
7
ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
wb.save('example.xls')
完整代碼如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
import xlwtfrom datetime import datetime
style0 = xlwt.easyxf('font: name Times New Roman, color-index red, bold on',num_format_str='#,##0.00')
style1 = xlwt.easyxf(num_format_str='D-MMM-YY')
wb = xlwt.Workbook()
ws = wb.add_sheet('A Test Sheet')
ws.write(0, 0, 1234.56, style0)
ws.write(1, 0, datetime.now(), style1)
ws.write(2, 0, 1)
ws.write(2, 1, 1)
ws.write(2, 2, xlwt.Formula("A3+B3"))
wb.save('example.xls')
程序執行結果如下:
更多Python知識,請關註:Python自學網!!
(推薦操作系統:windows7系統、Python 3.9.1,DELL G3電腦。)
❷ 怎麼使用python將一組數據寫入excel
方法一,xlwt:
importxlwt
workbook=xlwt.Workbook()
sheet=workbook.add_sheet("sheet1")
sheet.write(0,1,"test")
workbook.save("test.xls")
方法二,xlsxwriter:
importxlsxwriter
workbook=xlsxwriter.Workbook(file_name)
worksheet=workbook.add_worksheet(u"所有數據")
worksheet.write("A1",u"賬號ID")
workbook.close()