Reading Excel Files (Spreadsheets) in Python

Reading Excel Files (Spreadsheet) In Python

This is a detailed tutorial on reading excel files in python. Learn to extract the data from an Excel Workbook or spreadsheet using the xlrd module.

Reading an Excel File in Python

Spreadsheets are used worldwide to store tabular data. You can easily read and write these files in python. Recently, I’ve published an article on reading and writing CSV files. The spreadsheets are quite different from the CSV files. Therefore, I found another good module for specifically parsing the excel files.

In this tutorial, we’ll make use of the xlrd module to read spreadsheets in python. So, you need to install this module first. Simply run the following command to install this module.

Python Xlrd Module Installation

Example Excel File.

I’ll use the Excel File shown in the screenshot given below for an illustration of performing different reading operations on the file using the xlrd module in python.

Example Excel Sheet

This excel sheet is named demo.xlsx

Reading A Particular Cell Value

The following python code illustrates the use of the xlrd module to read a particular cell value from the example excel sheet.

Reading Cell Values From An Excel File Python

This way, you can fetch the value of any particular cell from any particular excel sheet of an Excel Workbook.

Extracting Number of Rows & Columns

This module also allows you to find the total number of rows and columns of the spreadsheet in an excel workbook. The following example illustrates this concept.

Extracting Number Of Rows & Columns From An Excel Sheet

Extracting Column Names

You can easily extract the name of all columns using the cell_value() function used in the first example by doing iteration over the number of columns. The following example illustrates the same.

Fetch All Column Names In A Spreadsheet Excel Example

Extracting Particular Column & Row Values

The following example illustrates how you can fetch all of the values for a particular column. Again, we’ll do this using iteration over the range of the number of rows. Here we’re reading the values for the second column, Full Name. As the column and row index starts from 0, so the index for the second column here will be 1.

Reading Particular Column Values Example Python

The example given below illustrates how you can fetch the values for an entire row. We need not do iteration here, rather we’ll make use of the row_values() function here. This function takes the row index as the parameter and returns the list of values of that particular row.

Fetching Row Values From Spreadsheet Python Example

Apart from the simple reading of the Excel files, you can do much more with the spreadsheets using the xlrd module. You can view the full documentation of the xlrd module here.

I hope you found this guide useful. If so, do share it with others who are willing to learn Python. If you have any questions related to this article, feel free to ask us in the comments section.

And do not forget to subscribe to WTMatter!

Subscribe To WTMatter!

Receive updates of our latest articles via email. Enter your email address below to get started.

Leave a Reply

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