
如本教程中所示,您需要
iter_rows在工作表实例上调用该方法,例如(对于openpyxl
2.5.14或更早版本):
>>> for row in ws.iter_rows('A1:C2'):... for cell in row:... print cell要么
>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2):... for cell in row:... print(cell)<Cell Sheet1.A1><Cell Sheet1.B1><Cell Sheet1.C1><Cell Sheet1.A2><Cell Sheet1.B2><Cell Sheet1.C2>
如错误消息所述,您正在对
Worksheet类型 调用它,这是行不通的。需要在一个 对象 上调用它:
op.worksheet.Worksheet.iter_rows() # wrong
对于openpyxl的旧版本,您可能需要确保在加载工作簿时启用迭代器-
请参见此线程。最新版本不需要此功能。
这是我刚刚在Python REPL(使用openpyxl 1.8.3)中测试的完整示例:
>>> import openpyxl as op>>> wb = op.load_workbook('/tmp/test.xlsx', use_iterators=True)>>> ws = wb.active>>> for row in ws.iter_rows():... for cell in row:... print cell... RawCell(row=1, column='A', coordinate='A1', internal_value=1.0, data_type='n', style_id='0', number_format='general')RawCell(row=1, column='B', coordinate='B1', internal_value=10.0, data_type='n', style_id='0', number_format='general')...欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)