M
M
mezigar2021-07-08 10:27:00
Python
mezigar, 2021-07-08 10:27:00

Why can't iterate through cellObj (openpyxl)?

You need to iterate over the sheet column and read certain data from the cells. The code has not reached the reading yet, since the cellObj object is non-iterable in my case. As I understand it, this is due to the fact that there are zero cells in the column, which are read as None. Here is the code that should run:

for cellObj in sheet['L']:
    #if  isinstance(cellObj, collections.abc.Iterable):   - если раскомментировать эту строчку, то код , находящийся ниже её не выполнится. Если же её убрать, то вылетит ошибка 
        for cell in cellObj:
            i = 1
            while cell.value != 'МТГ':
                i+=1
            print(i)

The actual error is TypeError: 'Cell' object is not iterable

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2021-07-08
@mezigar

cellObj is an instance of the openpyxl.cell.cell.Cell class, so you don't need to iterate over it

for index, cell in enumerate(sheet['L']):
    if cell.value == "МТГ":
        print(index)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question