Reading an Excel File using Python

The .xlsx is the expansion of the dominate archive that can store a lot of information in even structure, and many sorts of number juggling and legitimate estimation should be possible effectively in a dominate bookkeeping page. Now and then it is needed to peruse the information from the dominate archive utilizing Python script for the purpose of programming. Numerous modules exist in Python to peruse the dominant archive. A portion of the valuable modules are xlrd, openpyxl, and pandas. The ways of utilizing these modules to peruse the dominate record in Python have been displayed in this instructional exercise.

Python read dominate document

Dominate is an accounting page application which is created by Microsoft. It is an effectively available apparatus to put together, break down, and store the information in tables. It is generally utilized in various applications everywhere. From Analysts to CEOs, different experts use Excel for both fast details and genuine information crunching.

Dominate Documents

An Excel accounting page archive is known as an exercise manual which is saved in a record with .xlsx expansion. The primary line of the bookkeeping page is predominantly saved for the header, while the principal section distinguishes the examining unit. Every exercise manual can contain different sheets that are additionally called a worksheets. A case at a specific section and line is known as a cell, and every cell can incorporate a number or message esteem. The lattice of cells with information frames a sheet.

Utilizing xlrd module, one can recover data from an accounting page. For instance, perusing, composing or altering the information should be possible in Python. Likewise, the client may need to go through different sheets and recover information in view of certain standards or change a few lines and segments and do a great deal of work.

xlrd module is utilized to extricate information from a bookkeeping page.

Command to install xlrd module:

pip install xlrd

Input File:

Python

Code #1:  Extract a specific cell

# Reading an excel file using Python
import xlrd
# Give the location of the file
loc = ("path of file")
# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
print(sheet.cell_value(0, 0))

Output:

'NAME'

Code #2: Extract the number of rows

# Program to extract number
# of rows using Python
import xlrd
# Give the location of the file
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
# Extracting number of rows
print(sheet.nrows)

Output:

4

Code #3: Extract the number of columns

# Program to extract number of
# columns in Python
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0)
# Extracting number of columns
print(sheet.ncols)

Output:

3

Code #4 : Extracting all columns name

# Program extracting all columns
# name in Python
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
# For row 0 and column 0
sheet.cell_value(0, 0)
for i in range(sheet.ncols):
    print(sheet.cell_value(0, i))

Output:

NAME
SEMESTER
ROLL NO

Code #5: Extract the first column

# Program extracting first column
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
for i in range(sheet.nrows):
    print(sheet.cell_value(i, 0))

Output:

NAME
ALEX
CLAY
JUSTIN

Code #6: Extract a particular row value

# Program to extract a particular row value
import xlrd
loc = ("path of file")
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)
sheet.cell_value(0, 0)
print(sheet.row_values(1))

Output:

['ALEX', 4.0, 2011272.0]

Leave a Reply

Your email address will not be published. Required fields are marked *