pip install xlwt
或者anaconda的用户:conda install -c conda-forge xlwt
创建工作簿、工作表、写入数据、保存工作簿。
# 创建工作簿
workBook = xlwt.Workbook("UTF-8")
# 创建工作表
oneWorkSheet = workBook.add_sheet("sheet1")
# 写入数据(行, 列, 数据)
oneWorkSheet.write(0, 0, "hello")
oneWorkSheet.write(0, 1, "world")
# 保存数据,入参为文件路径
workBook.save("g:\myExcel.xls")
应用实例:九九乘法表
wk = xlwt.Workbook("UTF-8")
ws = wk.add_sheet("sheet1")
for row in range(0, 9):
for column in range(0, row + 1):
numRow = row + 1
numColumn = column + 1
ws.write(row, column, "%d * %d = %d" % (numRow, numColumn, (numRow * numColumn)))
wk.save("G:\easyExcel.xls")
(代码源于网络,非原创)