Maxime Campeau
Maxime Campeau

Reputation: 109

Openpyxl - How to find the amount of rows with data in an xlsx

for a script I created I would need to find a way to get the count of rows with values in an xlsx file. For instance :

enter image description here

The function would need to return, for example, x=9.

I've tried a few things but nothing does it. Any openpxyl master that could shed some light over this?

Thanks, once again!

Upvotes: 1

Views: 2506

Answers (2)

Joshua Fox
Joshua Fox

Reputation: 19675

As @Maxime Campeau said, there is an API for that in Openpyxl. But this is not the last non-blank row. This is the last row that was "never touched." For example, if someone edited, then deleted, row 1000, then max_row will be 1000 even if rows 100 and above are blank.

No library or API is available for this: If you want an accurate answer, take max_row and max_column and scan backwards until you hit a non-blank cell.

Upvotes: 1

Maxime Campeau
Maxime Campeau

Reputation: 109

As I ask my question, I come across the answer. For those interested :

wb = openpyxl.load_workbook('PATH')
sheet = wb['SHEET NAME']
nb_row = sheet.max_row
print(nb_row)

Upvotes: 1

Related Questions