close

How we can use iter_rows() in Python openpyxl package?

Hello Guys, How are you all? Hope You all Are Fine. Today We Are Going To learn about How we can use iter_rows() in Python openpyxl package in Python. So Here I am Explain to you all the possible Methods here.

Without wasting your time, Let’s start This Article.

Table of Contents

How we can use iter_rows() in Python openpyxl package?

  1. How we can use iter_rows() in Python openpyxl package?

    For older versions of openpyxl, you may need to ensure that you enable iterators when loading your workbook – see this thread. This isn't required for more recent versions.

  2. use iter_rows() in Python openpyxl package

    For older versions of openpyxl, you may need to ensure that you enable iterators when loading your workbook – see this thread. This isn't required for more recent versions.

Method 1

As shown in the tutorial, you need to call the iter_rows method on an instance of a worksheet

>>> for row in ws.iter_rows('A1:C2'):
...        for cell in row:
...            print cell

or

>>> 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>

As your error message states, you are calling it on the Worksheet type, which won’t work; it needs to be called on an object:

op.worksheet.Worksheet.iter_rows()  # wrong

For older versions of openpyxl, you may need to ensure that you enable iterators when loading your workbook – see this thread. This isn’t required for more recent versions.

Here’s a complete example which I just tested in the Python REPL (with 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')
...

Conclusion

It’s all About this issue. Hope all Methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which Method worked for you? Thank You.

Also, Read