I need to find the first empty row in an Excel file, i am currently using Openpyxl with Python. I couldn't find any method that does what i need so i am trying to make my own. This is my code:
book = load_workbook("myfile.xlsx")
ws = book.worksheets[0]
for row in ws['C{}:C{}'.format(ws.min_row,ws.max_row)]:
for cell in row:
if cell.value is None:
print cell.value
break
I am iterating through all cells in the "C" column and i am "breaking" if the cell is empty. The problem is that it won't break, it'll just keep print out "None" values.
Thanks
This way your loop will stop if it encounters any empty cell in a row.
If you want the row wo be completely empty you can use all
.
book = load_workbook("myfile.xlsx")
ws = book.worksheets[0]
for cell in ws["C"]:
if cell.value is None:
print cell.row
break
else:
print cell.row + 1
Update to the question in the comments:
ws["C"]
will get a slice from C1:CX
where X
is the last filled cell in any column. So if the C
column happens to be the longest column and every entry is filled you will only get cells with cell is not None
so you won't break out of the loop. If you didn't break out of the loop you will enter the else
block and since you looped till the last filled row, the first empty row will be the next one.