'How can I find the last non-empty row of excel using openpyxl 3.03?

How can I find the number of the last non-empty row of an whole xlsx sheet using python and openpyxl?

The file can have empty rows between the cells and the empty rows at the end could have had content that has been deleted. Furthermore I don't want to give a specific column, rather check the whole table.

For example the last non-empty row in the picture is row 13.

enter image description here

I know the subject has been extensively discussed but I haven't found an exact solution on the internet.



Solution 1:[1]

# Open file with openpyxl
to_be = load_workbook(FILENAME_xlsx)
s = to_be.active

last_empty_row = len(list(s.rows))
print(last_empty_row)
## Output: 13

s.rows is a generator and its list contains arrays of each rows cells.

Solution 2:[2]

openpyxl's class Worksheet has the attribute max_rows

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Charalamm
Solution 2 johnson