Effortless Python Code to Read from Excel File – Quick Guide

In today's data-driven world, tabs and tables are ubiquitous. Excel files, in particular, are a staple in many industries, packed with valuable information. When you need to extract data from these files, you can employ Python with its powerful libraries. Reading from an Excel file in Python can seem daunting initially, but with the right tools, it's a straightforward process.

Your Guide to Reading Excel (xlsx) Files in Python
Your Guide to Reading Excel (xlsx) Files in Python

One of the most efficient ways to handle this task is by using the `pandas` library in Python. `pandas` provides a plethora of data manipulation functions and structures, including `DataFrame`, a two-dimensional labeled data structure. The library has built-in functionalities to read and write Excel files, which makes it a convenient choice.

Python Cheatsheet for Beginners | Learn Python Fast
Python Cheatsheet for Beginners | Learn Python Fast

Installing the Required Libraries

Before we delve into reading Excel files, you need to ensure that the `pandas` and `openpyxl` libraries are installed. The `openpyxl` library provides support for the `pandas` `read_excel` function. If you haven't installed these libraries, you can do so using pip:

All Important Python Functions for Beginners (Complete Cheat Sheet)
All Important Python Functions for Beginners (Complete Cheat Sheet)

  • pip install pandas
  • pip install openpyxl

Reading Excel Files with `pandas`

How to read Multiple Columns from CSV File in Python? With Source Code
How to read Multiple Columns from CSV File in Python? With Source Code

The `read_excel` function, part of `pandas`, is a breeze to use. It allows you to read an Excel file into a `DataFrame` object. Here's a simple example:

import pandas as pd
df = pd.read_excel('file.xlsx')
print(df)

Specifying the Sheet

Python Unit 5 Cheat Sheet 🐍 | File Handling & OOP Basics (AKTU)
Python Unit 5 Cheat Sheet 🐍 | File Handling & OOP Basics (AKTU)

When you have multiple sheets in your Excel file, you can specify which sheet to read using the `sheet_name` parameter. You can pass either a string (the name of the sheet) or an integer (the index of the sheet).

df = pd.read_excel('file.xlsx', sheet_name='Sheet1')
or
df = pd.read_excel('file.xlsx', sheet_name=1)

Working with Specified Columns

Python
Python

Not all data in an Excel file is relevant for your analysis. That's why `pandas` allows you to specify which columns to read using the `usecols` parameter. This parameter accepts either a list of column labels or a list of column indices. For instance:

Using Column Labels

the calendar in python code is shown with red arrows pointing to it and black background
the calendar in python code is shown with red arrows pointing to it and black background
Master Data Export in Python – One Cheat Sheet to Rule Them All!
Master Data Export in Python – One Cheat Sheet to Rule Them All!
an info sheet with the words 10 python one - liners that will blow your mind
an info sheet with the words 10 python one - liners that will blow your mind
a panda bear sitting on top of a table next to a sign that says, time series analysis date range
a panda bear sitting on top of a table next to a sign that says, time series analysis date range
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀
SQL to Python to Excel Cheat Sheet for Data Analysis
SQL to Python to Excel Cheat Sheet for Data Analysis
the diagram shows how to use python commands for web pages and other contents, including data
the diagram shows how to use python commands for web pages and other contents, including data
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
30-Day Python Learning Plan for Beginners | Complete Python Roadmap to Learn Coding Fast
the text file is open and ready to be read in this screenshote screen
the text file is open and ready to be read in this screenshote screen
MLTut
MLTut
Best Practices to Write Clean Python Code
Best Practices to Write Clean Python Code
Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews
Python Cheat Sheet 🚀 | Beginner to Advanced | Save This for Coding & Interviews
Python File Handling Explained for Beginners (Complete Guide)
Python File Handling Explained for Beginners (Complete Guide)
Ezekiel (@ezekiel_aleke) on X
Ezekiel (@ezekiel_aleke) on X
an image of a computer screen with text and images on the page, which reads python cheat
an image of a computer screen with text and images on the page, which reads python cheat
Python Programming, Python
Python Programming, Python
Learn python
Learn python
Read and Write CSV Files in Python
Read and Write CSV Files in Python
Python Learning Path for Beginners 2026 🚀
Python Learning Path for Beginners 2026 🚀

Let's say you're only interested in columns 'A', 'C', and 'E'.

df = pd.read_excel('file.xlsx', usecols=['A', 'C', 'E'])

Using Column Indices

Alternatively, you can use column indices. The first column is indexed as 0, the second as 1, and so forth.

df = pd.read_excel('file.xlsx', usecols=[0, 2, 4])

Once you're comfortable with these basics, you can delve into more complex operations like reading and writing multiple sheets, skipping rows or columns, and handling merged cells.

Remember, working with data doesn't stop at reading it. Once you've extracted the data, you can manipulate and analyze it using the vast `pandas` functionality. With that, you're one step closer to deriving valuable insights from your data.

Related Articles

Python Dict Sample Code Python Debugging For Beginners Snowflake Sample Code Sample Run Python Sample Python Code For Lambda Function S3 Sample Python Interview Questions Python Random Sample Dictionary Is Python Good For Data Visualization Simple Python Backend What Is Snowflake Code