Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm using openpyxl package in Python(Canopy) to use excel files. We have this tutorial in this link : 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>

How we can import openpyxl.worksheet.Worksheet.iter_rows() method in python? I used this code:

import openpyxl as op
ms = op.load_workbook('mtest.xlsx')

ws = ms.active

op.worksheet.Worksheet.iter_rows()

This code returns:

type object 'Worksheet' has no attribute 'iter_rows' 

What is the problem?

like image 623
Eghbal Avatar asked Apr 22 '15 08:04

Eghbal


People also ask

What is Iter_rows in Python?

The iter_rows function return cells from the worksheet as rows. The example iterates over data row by row. for row in sheet. iter_rows(min_row=1, min_col=1, max_row=6, max_col=3):

How do I get row values in openpyxl?

Our aim is to display the values of all the rows of a particular column of the active sheet. Step1: Firstly, let's import openpyxl library to our program. Step2: Load the Excel workbook to the program by specifying the file's path. Here, we will use the load_workbook() method of the openpyxl library for this operation.

How do I get Max rows in openpyxl?

To find the max row and column number from your Excel sheet in Python, use sheet. max_row and sheet. max_column attributes in Openpyxl. Note - If you update a cell with a value, the sheet.


1 Answers

As shown in the tutorial, you need to call the iter_rows method on an instance of a worksheet, for example (for openpyxl 2.5.14 or earlier):

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

See also this example in another answer.

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')
...
like image 115
DNA Avatar answered Oct 23 '22 04:10

DNA