ItSolutionStuff.com

How to Read Excel File in Python?

By Hardik Savani • October 30, 2023
Python

Hello,

This simple article demonstrates of how to read excel file in python. you'll learn python read excel file example. We will look at an example of how to get data from excel file in python. I would like to show you how to read data from excel file in python. you will do the following things for how to extract data from excel using python.

There are several modules and ways to read excel files in python. i will give you simple two examples using pandas and openpyxl to read and get excel file data. so let's see both examples one by one.

You can use these examples with python3 (Python 3) version.

I simply created data.xlsx file with content as like below, we will use same file for both example:

demo.xlsx

Example 1: using pandas

main.py

# import pandas library as pd
import pandas as pd
   
# get by default 1st sheet of demo excel file
data = pd.read_excel('demo.xlsx')
   
print(data)

Output:

   ID             Name              Email
0   1    Hardik Savani   hardik@gmail.com
1   2  Vimal Kashiyani    vimal@gmail.com
2   3   Harshad Pathak  harshad@gmail.com

Example 2: using openpyxl

main.py

import openpyxl
  
# Define variable to load the dataframe
dataframe = openpyxl.load_workbook("demo.xlsx")
   
# Define variable to read sheet
data = dataframe.active
   
# Display Row Data
for row in range(0, data.max_row):
    for col in data.iter_cols(1, data.max_column):
        print(col[row].value)

Output:

ID
Name
Email
1
Hardik Savani
hardik@gmail.com
2
Vimal Kashiyani
vimal@gmail.com
3
Harshad Pathak
harshad@gmail.com

I hope it can help you...

Tags: Python
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

📺 Subscribe on YouTube

We Are Recommending You

Python Read and Write a JSON File Example

Read Now →

Python Create JSON File from Dict Example

Read Now →

Python Create JSON File from List Example

Read Now →

How to Write a JSON File in Python?

Read Now →

How to Read a JSON File in Python?

Read Now →

How to Read a CSV File in Python?

Read Now →

How to Write Multiple Rows in CSV using Python?

Read Now →

Python Write CSV File from List Example

Read Now →

How to Read Text File Line by Line in Python?

Read Now →

Python Generate Text File from List Example

Read Now →

Python Create Zip Archive from Directory Example

Read Now →