python处理Excel - xlrd xlwr openpyxl
1 xlrd和xlwt
Todo: 使用xlrd和xlwt读写Excel文件的方法和示例代码,待续。。。
参考链接:
:https://www.blog.pythonlibrary.org/2014/03/24/creating-microsoft-excel-spreadsheets-with-python-and-xlwt/
前期使用xlrd和xlwt读写excel表格,现写入excel时出现问题:
ValueError: row index was 65536, not allowed by .xls format
xlwt只能处理.xls格式的Excel,即2003之前的版本,Excel2003只能支持65535行数据,实际应用超出该范围,因此抛出错误[1]。
2 openpyxl
Sample code:
from openpyxl import Workbookwb = Workbook()# grab the active worksheetws = wb.active# Data can be assigned directly to cellsws['A1'] = 42# Rows can also be appendedws.append([1, 2, 3])# Python types will automatically be convertedimport datetimews['A2'] = datetime.datetime.now()# Save the filewb.save("sample.xlsx")
: https://openpyxl.readthedocs.io/en/stable/tutorial.html
核心代码:
# Create a workbookfrom openpyxl import Workbookwb = Workbook()ws = wb.active# Playing with data## Accessing one cellws['A4'] = 4c = ws['A4']d = ws.cell(row=4, column=2, value=10)for x in range(1,101): for y in range(1,101): ws.cell(x, y, value)## Accessing many cellscell_range = ws['A1':'C2']# Saving to a filewb.save('balances.xlsx')
参考链接:
[1] : https://stackoverflow.com/questions/45741670/valueerror-row-index-was-65536-not-allowed-by-xls-format