pythonexcelrowopenpyxlskip

OpenPyXL - How to skip row if cell.value is None


I've read plenty of answers here regarding empty rows, but somehow my attempts to adapt those solutions to my script has failed.

I'm reading sheets in excel files using OpenPyXL and loading it's part which is to be processed to DataFrame (first read each row to list of lists and then convert it to DataFrame). The thing is that I'm looking for elegant solution to skip row if the cell.value of the first cell is None

I iterate through rows with the below code:

for row in ws.iter_rows(min_col=adres[0], min_row=adres[1], max_col=adres[2], max_row=adres[3]):
    data_rows.append([cell.value for cell in row]) 

Solution

  • Thank you, the solution is so simple I'm ashamed I was even asking :)

    Here it is working nicely:

    for row in ws.iter_rows(min_col=adres[0], min_row=adres[1], max_col=adres[2], max_row=adres[3]):
        if row[0].value is not None:
            data_rows.append([cell.value for cell in row])
        else: continue