Issues using iter_rows

3,044 views
Skip to first unread message

zakp...@gmail.com

unread,
Jun 21, 2012, 10:07:36 AM6/21/12
to openpyx...@googlegroups.com
When trying to use iter_rows I get an error saying that "Worksheet object has no attribute iter_rows". I have the latest version of both openpyxl (1.5.8) and python (2.7.3) using office 2003 on windows 7. I was using the snippet of code below when the error cropped up:

from openpyxl.reader.excel import load_workbook
from openpyxl.workbook import Workbook
import time, sys

***other code***

col_name = 'A'
start_row
= 1
end_row
= 99

range_expr
= "{col}{start_row}:{col}{end_row}".format(
    col
=col_name, start_row=start_row, end_row=end_row)

for (time_cell,) in ws.iter_rows(range_string=range_expr):
    print time_cell.value.hour 

There isn't anything extra I have to import for this to work is there? 

Eric Gazoni

unread,
Jun 21, 2012, 10:23:57 AM6/21/12
to openpyx...@googlegroups.com
Don't forget to load_workbook with iterators support :

workbook = load_workbook('/path/to/file', use_iterators=True)

Cheers,
Eric


Le 21/06/12 16:07, zakp...@gmail.com a écrit :

Zak

unread,
Jun 21, 2012, 10:35:51 AM6/21/12
to openpyx...@googlegroups.com
Ah, something so simple, thanks.

Another question, while I have an active thread, how would I go about getting values from a column? I know using iter_rows like this will pass through a column until the end of the file. My code as following is supposed to pass through column C and pull out integer values until there is no more data: 

   address = []
   col_name = 'C'
   start_row = 4
   end_row = ws.get_highest_row()+1
   range_expr = "{col}{start_row}:{col}{end_row}".format(col=col_name, start_row=start_row, end_row=end_row)
   for cell in ws.iter_rows(range_string=range_expr):
      if str(cell.value).isdigit:
         print 'it lives!!'
      else:
         print 'it doesnt!!'
   return address

But crashes at the if statement. saying that 'tuple' object has no attribute 'value'. What makes no sense to me is that the documentation uses cell.value throughout. I have a feeling im missing something terribly simple again.

Eric Gazoni

unread,
Jun 21, 2012, 10:42:48 AM6/21/12
to openpyx...@googlegroups.com
iter_rows returns a tuple of cell, one per row.

if you have a range that is 1-col wide, then you can do

for row in ws.iter_rows(...):
    cell = row[0]
    if str(cell.value)......

Cheers,
Eric

Le 21/06/12 16:35, Zak a écrit :

Zak

unread,
Jun 21, 2012, 10:48:56 AM6/21/12
to openpyx...@googlegroups.com
Hm that doesnt seem to work, it gives "IndexError: tuple index out of range" at the line:

cell = row[0] 

Eric Gazoni

unread,
Jun 21, 2012, 10:50:01 AM6/21/12
to openpyx...@googlegroups.com
that means your iter_rows is not retrieving anything, try to print "row" and see if that looks like what you expect.
Le 21/06/12 16:48, Zak a �crit�:
Hm that doesnt seem to work, it gives "IndexError: tuple index out of range" at the line:

cell = row[0]�


Zak

unread,
Jun 21, 2012, 10:55:59 AM6/21/12
to openpyx...@googlegroups.com
Hmm, printing the row (even after manually changing where the program is going in the spreadsheet) always shows an empty set of parens -> ()

Zak

unread,
Jun 21, 2012, 3:50:55 PM6/21/12
to openpyx...@googlegroups.com
is there any reason that iter_rows wouldnt be returning anything??

Zak

unread,
Jun 22, 2012, 10:09:53 AM6/22/12
to openpyx...@googlegroups.com
found a fix, all i needed was:

range_expr = 'C:C'
for row in ws.iter_rows(row_offset=4):
            '''code'''
Reply all
Reply to author
Forward
0 new messages