How to Open and Read Xlsx File in Python?
Hello Guys,
In this tutorial, we will go over the demonstration of how to open and read xlsx file in python. you can see how to open xlsx file in python. This article goes in detailed on how to open xlsx file in python using xlrd. you will learn python read xlsx file line by line.
There are several modules and ways to read xlsx files in python. i will give you simple two examples using pandas and openpyxl to read and get xlsx 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...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Python Read and Write a JSON File Example
- Python Create JSON File from List Example
- How to Get Column Names from CSV File in Python?
- How to Read a CSV File in Python?
- How to Write CSV File in Python?
- Python Read Text File into List Example
- How to Read Text File Line by Line in Python?
- How to Read a Text File in Python?
- Python List Print All Elements Except First Example
- Python Get First Date of Next Month Example
- Python Create Zip Archive from Directory Example
- Python List All Dates Between Two Dates Example