发布于2019-09-11 19:45 阅读(284) 评论(0) 点赞(19) 收藏(4)
我正在使用openpyxl
包Python(Canopy)
来使用excel文件。我们在此链接中有本教程:LINK
you can also use the openpyxl.worksheet.Worksheet.iter_rows() method:
>>> tuple(ws.iter_rows('A1:C2'))
((<Cell Sheet1.A1>, <Cell Sheet1.B1>, <Cell Sheet1.C1>),
(<Cell Sheet1.A2>, <Cell Sheet1.B2>, <Cell Sheet1.C2>))
>>> for row in ws.iter_rows('A1:C2'):
... for cell in row:
... print cell
<Cell Sheet1.A1>
<Cell Sheet1.B1>
<Cell Sheet1.C1>
<Cell Sheet1.A2>
<Cell Sheet1.B2>
<Cell Sheet1.C2>
我们如何openpyxl.worksheet.Worksheet.iter_rows()
在python中导入方法?我用过这段代码:
import openpyxl as op
ms = op.load_workbook('mtest.xlsx')
ws = ms.active
op.worksheet.Worksheet.iter_rows()
此代码返回:
type object 'Worksheet' has no attribute 'iter_rows'
问题是什么?
如教程所示,您需要iter_rows
在工作表的实例上调用该方法,例如:
>>> 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')
...
作者:黑洞官方问答小能手
链接:https://www.pythonheidong.com/blog/article/108055/d8adcee7cbb59d1ee68c/
来源:python黑洞网
任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任
昵称:
评论内容:(最多支持255个字符)
---无人问津也好,技不如人也罢,你都要试着安静下来,去做自己该做的事,而不是让内心的烦躁、焦虑,坏掉你本来就不多的热情和定力
Copyright © 2018-2021 python黑洞网 All Rights Reserved 版权所有,并保留所有权利。 京ICP备18063182号-1
投诉与举报,广告合作请联系vgs_info@163.com或QQ3083709327
免责声明:网站文章均由用户上传,仅供读者学习交流使用,禁止用做商业用途。若文章涉及色情,反动,侵权等违法信息,请向我们举报,一经核实我们会立即删除!