Python and openpyxl: Counting non-empty rows in Excel made easy

Dhruv Singhal
2 min readMay 6, 2023

--

Are you tired of using the max_row function in openpyxl to count the number of rows in your Excel file, only to find that it counts all rows, including the empty ones? If so, this article is for you. In this tutorial, we’ll show you how to count non-empty rows in Excel using Python’s openpyxl library.

The openpyxl library is a popular Python library for working with Excel files. It provides a simple interface for reading and writing data to Excel files, as well as a range of useful functions for manipulating data.

To count non-empty rows in Excel using openpyxl, we need to iterate over each row in the worksheet and check if it has any non-empty cells. If it does, we’ll increment a counter to keep track of the number of non-empty rows.

Here’s the code to count non-empty rows in Excel using openpyxl:

import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
ws = wb.active
count = 0
for row in ws:
if not all([cell.value == None for cell in row]):
count += 1
print(count)

Let’s break down the code:

  1. We import the openpyxl library and load our Excel file using the load_workbook function.
  2. We get the active worksheet using the active property of the workbook object.
  3. We initialize a counter variable called count to 0.
  4. We iterate over each row in the worksheet using a for loop.
  5. For each row, we check if all the cells are empty by using a list comprehension to check if each cell’s value is None. If all the cells are empty, we skip the row. Otherwise, we increment the count variable by 1.
  6. Finally, we print the count variable, which gives us the number of non-empty rows in the Excel file.

Alternatively, you can use a single-line list comprehension to count the non-empty rows in the worksheet:

import openpyxl
wb = openpyxl.load_workbook('example.xlsx')
ws = wb.active
count = len([row for row in ws if not all([cell.value == None for cell in row])])
print(count)

This code accomplishes the same thing as the previous code but is more concise.

In summary, counting non-empty rows in Excel using openpyxl is easy. By iterating over each row in the worksheet and checking for non-empty cells, we can accurately count the number of non-empty rows in the file. No more struggling with the max_row function that counts all rows, including the empty ones.

We hope this tutorial helps you with your data analysis and Excel file manipulation tasks in Python. Happy coding!

Any suggestions and comments would be greatly appreciated. If you found this article helpful, please like and share it with others. Don’t forget to follow me to stay up-to-date on my latest articles. Thank you for taking the time to read this!

--

--

Dhruv Singhal
Dhruv Singhal

Written by Dhruv Singhal

Data engineer with expertise in PySpark, SQL, Flask. Skilled in Databricks, Snowflake, and Datafactory. Published articles. Passionate about tech and games.

No responses yet