用于以Excel 2007+ XLSX文件格式编写文件
其使用流程,与你使用excel流程一致,只不过将你主步骤分解成了一个个对象实例来操作,通过引用实现操作关联
import xlsxwriter
# 1.创建工作簿
workbook = xlsxwriter.Workbook('Expenses01.xlsx')
# 2.添加工作表
worksheet = workbook.add_worksheet()
# 2.准备数据
expenses = (
['Rent', 1000],
['Gas', 100],
['Food', 300],
['Gym', 50],
)
# 3.向工作表写入数据
row,col = 0,0
for item,cost in (expenses) :
worksheet.write(row,col,item)
worksheet.write(row,col+1,cost)
row +=1
worksheet.write(row,0,'Total')
worksheet.write(row,1,'=SUM(B1:B4)')
# 4. 关闭
workbook.close()
https://xlsxwriter.readthedocs.io/contents.html