Reading an excel file using Python

Configurare noua (How To)

Situatie

One can retrieve information from a spreadsheet. Reading, writing, or modifying the data can be done in Python can be done in using different methods.

Solutie

Pasi de urmat

Required Module

pip install xlrd

Reading an excel file using Python using Pandas

In this method, We will first import the Pandas module then we will use Pandas to read our excel file.

# import pandas lib as pd
import pandas as pd

# read by default 1st sheet of an excel file
dataframe1 = pd.read_excel(‘book2.xlsx’)

print(dataframe1)

 

output:

 

Reading an excel file using Python using openpyxl

The load_workbook() function opens the Books.xlsx file for reading. This file is passed as an argument to this function. The object of the dataframe.active has been created in the script to read the values of the max_row and the max_column properties. These values are used in the loops to read the content of the Books2.xlsx file.

import openpyxl
# Define variable to load the dataframe
dataframe = openpyxl.load_workbook(“Book2.xlsx”)
# Define variable to read sheet
dataframe1 = dataframe.active
# Iterate the loop to read the cell values
for row in range(0, dataframe1.max_row):
for col in dataframe1.iter_cols(1, dataframe1.max_column):
print(col[row].value)

Tip solutie

Permanent

Voteaza

(6 din 12 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?