Open In App

Reading an excel file using Python openpyxl module

Last Updated : 28 Jul, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Openpyxl is a Python library for reading and writing Excel (with extension xlsx/xlsm/xltx/xltm) files. The Openpyxl Module allows Python programs to read and modify Excel files. For example, users might have to go through thousands of rows and pick out a few handfuls of information to make small changes based on some criteria. Using Openpyxl module, these tasks can be done very efficiently and easily.

Installation

Use this command to install openpyxl module

pip install openpyxl

Example Excel File (demo.xlsx)

Let’s assume your Excel file contains student records in the following format:

Accessing a Particular Cell

You can fetch a specific cell's value using its row and column index.

Python
import openpyxl

path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active

# Access cell at row 1, column 1
cell_obj = sheet_obj.cell(row=1, column=1)
print(cell_obj.value)

Output:

STUDENT 'S NAME

Explanation:

  • load_workbook(path) loads the Excel workbook.
  • wb_obj.active retrieves the active sheet.
  • cell(row, column) returns a cell object by position.
  • cell_obj.value returns the content of the cell.

Get Total Number of Rows

To determine how many rows your sheet contains, use max_row. This is useful for iterating through entire datasets.

Python
import openpyxl

path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active

print(sheet_obj.max_row)

Output

6

Explanation: max_row returns the total number of rows with data in the sheet.

Get Total Number of Columns

You can use max_column to find out the number of columns. This helps when reading header fields or iterating over columns.

Python
import openpyxl

path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active

print(sheet_obj.max_column)

Output

4

Explanation : max_column gives the count of the rightmost column that contains data.

This example prints all column titles (first row values). It helps when you want to inspect or label your data columns.

Python
import openpyxl

path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active

for i in range(1, sheet_obj.max_column + 1):
    print(sheet_obj.cell(row=1, column=i).value)

Output

STUDENT 'S NAME
COURSE
BRANCH
SEMESTER

Explanation:

  • Loops through columns in the first row.
  • Prints each header value using cell(row=1, column=i).value.

To list all entries from the first column (e.g., student names). Use max_row to iterate through all rows in column 1.

Python
import openpyxl

path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active

for i in range(1, sheet_obj.max_row + 1):
    print(sheet_obj.cell(row=i, column=1).value)

Output:

STUDENT 'S NAME
ANKIT RAI
RAHUL RAI
PRIYA RAI
AISHWARYA
HARSHITA JAISWAL

Explanation:

  • Iterates from row 1 to the last row.
  • Prints values from column 1 using cell(row=i, column=1).value.

To print all values from a specific row, such as the 2nd. This is helpful when you want to extract a full record.

Python
import openpyxl

path = "C:\\Users\\Admin\\Desktop\\demo.xlsx"
wb_obj = openpyxl.load_workbook(path)
sheet_obj = wb_obj.active

for i in range(1, sheet_obj.max_column + 1):
    print(sheet_obj.cell(row=2, column=i).value, end=" ")

Output: 

ANKIT RAI B.TECH CSE 4

Explanation:

  • Loops through all columns in row 2.
  • Prints each value using cell(row=2, column=i).value.

Practice Tags :

Similar Reads