Reading an excel file using Python openpyxl module
Last Updated :
28 Jul, 2025
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.
Print Column Headers
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.
Print All Values of the First Column
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.
Print Values from a Specific Row
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.
What will be the output of the following openpyxl code if the cell A1 has value 'Python'?
Python
import openpyxl
wb = openpyxl.load_workbook("demo.xlsx")
sheet = wb.active
print(sheet.cell(row=1, column=1).value)
-
Prints the cell’s row number
-
-
-
Explanation:
cell(row=1, column=1).value accesses the value in cell A1, which is 'Python'.
Which method in openpyxl is used to determine the number of rows in an Excel sheet?
Explanation:
sheet.max_row returns the number of rows with data in the Excel sheet.
What will this code output if there are 4 columns in the Excel sheet?
Python
print(sheet_obj.max_column)
Explanation:
max_column gives the total number of columns with data.
How do you loop through the first column values in openpyxl?
-
Using range(1, sheet.max_column + 1)
-
-
Using range(1, sheet.max_row + 1) and setting column=1
-
Looping through sheet.rows directly
Explanation:
To get all values in the first column, iterate rows and fix column=1.
Which openpyxl method reads a specific row of values from the Excel sheet?
-
sheet.cell(row=i, column=j) in a column loop
-
-
-
Explanation:
To read a full row, you loop through columns with a fixed row index using cell().
Quiz Completed Successfully
Your Score : 2/5
Accuracy : 0%
Login to View Explanation
1/5
1/5
< Previous
Next >
Explore
Python Fundamentals
Python Data Structures
Advanced Python
Data Science with Python
Web Development with Python
Python Practice