简体   繁体   中英

openpyxl iterate through cells of column => TypeError: 'generator' object is not subscriptable

i I want to loop over all values op a column, to safe them as the key in a dict. As far as i know, everything is ok, but python disagrees.

So my question is: "what am i doing wrong?"

>>> wb = xl.load_workbook('/home/x/repos/network/input/y.xlsx')
>>> sheet = wb.active
>>> for cell  in sheet.columns[0]:
...     print(cell.value)
... 
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'generator' object is not subscriptable

I must be missing something, but after a few hours of trying, I must throw in the towle and call in the cavalecry ;)

thanks in advance for all the help!

===================================================================

@Charlie Clark: thanks for taking the time to answer. The thing is, I need the first column as keys in a dict, afterwards, I need to do

for cell  in sheet.columns[1:]:

so, while it would resolve my issue, it would come back to me a few lines later, in my code. I will use your suggestion in my code to extract the keys.

The question I mostly have is:

why doesn't it work, I am sure I used this code snippet before and this is also how, when googling, people are suggesting to do it.

==========================================================================

>>> for column in ws.iter_cols(min_col = 2):
...     for cell in column:
...             print(cell.value)

goes over all columns of the sheet, except the first. Now, I still need to exclude the first 3 rows

ws.columns returns a generator of columns because this is much more efficient on large workbooks, as in your case: you only want one column. Version 2.4 now provides the option to get columns directly: ws['A']

apparently, the openpyxl module got upgraded to 2.4, without my knowledge.

>>> for col in ws.iter_cols(min_col = 2, min_row=4):
...     for cell in col:
...         print(cell.value)

should do the trick. I posted this in case other people are looking for the same answer.

iter_cols(min_col=None, max_col=None, min_row=None, max_row=None)[source]

Returns all cells in the worksheet from the first row as columns.

If no boundaries are passed in the cells will start at A1.

If no cells are in the worksheet an empty tuple will be returned.
Parameters: 

    min_col (int) – smallest column index (1-based index)
    min_row (int) – smallest row index (1-based index)
    max_col (int) – largest column index (1-based index)
    max_row (int) – smallest row index (1-based index)

Return type:    

generator

I'm a newbie, but I came up with the following code based on openpyxl document to print out the cell contents for a column:

x = sheet.max_row + 1

for r in range(1,x):
    d=sheet.cell(row=r,column=2)
    print(d.value)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM