被xlrd/xlwt两个库折腾的一头雾水的时候,发现了处理xlsx格式的openpyxl库,试验了一下,非常好用。
可以把DataFrame里生成的表格导成Excel。
上一段测试代码:
from openpyxl import Workbook
wb = Workbook()
# grab the active worksheet
ws = wb.active
# Data can be assigned directly to cells
ws['A1'] = 42
ws['b1'] = 45
# Rows can also be appended
ws.append([1, 2, 3])
# Python types will automatically be converted
import datetime
ws['A2'] = datetime.datetime.now()
ws.append([666,777, 999])
# Save the file
#保存
wb.save(filename="d:/py/test_to_excel.xlsx")
生成的Excel表格:
网友评论