VOOZH about

URL: https://www.geeksforgeeks.org/python/python-reading-excel-file-using-openpyxl-module/

⇱ Reading an excel file using Python openpyxl module - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

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:

πŸ‘ Image

Accessing a Particular Cell

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

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.

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.

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.

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.

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.

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.
Comment