Python Read Excel file

Facebooktwitterredditpinterestlinkedinmailby feather

Python Read Excel file

Excel is the spreadsheet application for Window, which is developed by Microsoft. The Excel stores data in the tabular form. It provides easy access to analyze and maintain the data. It is widely used in many different software fields.

An excel spreadsheet document is saved in the file with .xlsx extension. The first row of the spreadsheet commonly reserved for the header, while the first column identifies the sampling unit.

A box at a specific column and row is called a cell, and each cell can include a number or text value. The grid of cells with data forms a sheet.

Read from the Excel file

Python provides facilities to read, write, and modify the excel file. The xlrd module is used to work with the excel file. Python does not come with the xlrd module. First, we need to install this module by using following command:

Creating a Workbook:

A workbook holds all the data in the excel file. Let’s consider the following input (excel) file.

Python Read from the Excel file

Consider the following code:

In the above example, first, we imported the xlrd module and declared the loc variable, which holds the location of the file. Then we opened the working workbook from the excel file.

  • Extract the number of columns and rows

Output:

  • Extracting all column name

Output:

  • Extracting a particular row value

Output:

Reading from the Pandas

Pandas is an open-source Python library which is built on the top of the NumPy library. First, we need to import the pandas module. It supports both xls and xlsx extensions from the URL. Consider the following example:

Output:

  • Exacting rows name

Output

Reading from the openpyxl

Python provides openpyxl which can perform multiple operations on the excel files such as reading, writing, and arithmetic operations. We need to install openpyxl using pip from the command line.

Output:

Facebooktwitterredditpinterestlinkedinmailby feather