Python Open and Read Xlsx File Example

Published On: 11/03/2023 | Category: Python


Hi Dev,

This article will provide example of how to open xlsx file in python. this example will help you how to open xlsx file in python using xlrd. you will learn python read xlsx file line by line. let’s discuss about how to open and read xlsx file in python.

Here, i will describe you of there are many ways to read xlsx files in python. two examples using pandas and openpyxl to read and get xlsx file data. so let's see both examples one by one.

I basically created data.xlsx we will use same file for both example:

So let's see bellow example:

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('data.xlsx')
   
print(data)
Output
   ID             Name              Email
0   1   Bhavesh Sonagra   [email protected]
1   2   Nikhil Thumar     [email protected]
2   3   Piyush Kamani     [email protected]

Example 1: Using openpyxl

main.py
import openpyxl
  
# Define variable to load the dataframe
dataframe = openpyxl.load_workbook("data.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
Bhavesh Sonagra
[email protected]
2
Nikhil Thumar
[email protected]
3
Piyush Kamani
[email protected]

It will help you....