Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get empty cell coordinates with openpyxl

I work at a script to extract some very messy data from a huge excel file and I run into a bump. There is any way to get coordinates from an empty cell with openpyxl?

This will return an error saying EmptyCell has no row/column attribute

for row in sheet.iter_rows():
    for cell in row:
        if cell.column is ... :
           print(cell.value)

I cannot skip the empty cells, as there are empty cells in some columns that have to taken into account and extracted as such in the new file. Also, I cannot fill the empty cells because the original file is huge and its opened read only because of this.

There is any way to do this with openpyxl or I should try to use another library?

like image 627
Dorin.D Avatar asked Mar 05 '26 05:03

Dorin.D


2 Answers

EmptyCell's are special cells in the read-only context: they are fillers created where there are no cells in the original XML so that rows from a range are of equal length. As such they do not really exist and hence do not have coordinates. If you need their coordinates then you can simply use enumerate to count for you while looping.

like image 89
Charlie Clark Avatar answered Mar 06 '26 19:03

Charlie Clark


for row in sheet.iter_rows():  
       for cell in row:  
        if cell.value is None :  
           print(str(cell.column)+str(cell.row))  
like image 27
ashim Avatar answered Mar 06 '26 17:03

ashim



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!