Updated March 29, 2023
Definition of Python xlrd
Python xlrd retrieves data from a spreadsheet using the xlrd module. It is used to read, write, or modify data. Furthermore, the user may be required to navigate sheets on certain criteria, as well as modify some rows, and columns and perform other tasks. This module is used to extract data from the sheet. And it is basically used to read and write and extract data from sheets.
What is Python xlrd?
- Furthermore, the user may be required to navigate through several sheets and extract data based on certain criteria.
- The attributes can be used to determine rows in the sheet. By default, all rows are padded with same size, but if we want to skip the empty columns at the end, use the ragged rows parameter when calling the open workbook function.
- We need to install xlrd package by using the pip command. Without installing the xlrd package we cannot use the same in our code. It is very useful and important in python.
How to Use Python xlrd?
We need to install xlrd package by using the pip command. Xlrd module is not coming by default at the time of installing the python package in our system.
1. In the first step, we are installing the xlrd module by using the pip command. We can install the xlrd module in any operating system on which python is installed. In the below example, we are installing the xlrd module on UNIX systems.
Code:
pip install xlrd
Output:
2. After installing all the modules we are opening the python shell by using the python3 command.
Code:
python3
Output:
3. After login into the python shell in this step, we are checking xlrd package is installed in our system. To use the xlrd module in our program we need to import the same in our code by using the import keyword. Without importing the xlrd module we cannot use the same in our code.
Code:
import xlrd
print (xlrd)
Output:
4. After checking the installation of the xlrd package in this step we are creating the spreadsheet file name as test.xlsx.
5. After creating the spreadsheet in this step we are extracting the specific cell from the sheet as follows. To extract the cell first we imported the xlrd module by using the import keyword. After importing the xlrd module in the next step we are opening the spreadsheet file by using the open workbook method.
After opening the sheet we extract the cell as per the spreadsheet index, we are retrieving the 0 index cell as follows.
Code:
import xlrd
file_loc = ("test.xlsx")
py_xl = xlrd.open_workbook (file_loc)
py_sheet = py_xl.sheet_by_index (0)
print (py_sheet.cell_value (0, 0))
Output:
How to Install XLRD in Python?
We are installing by using the pip command. In the below example, we are installing it on the windows system as follows. The below steps show how to install the xlrd module on the windows system.
1. In the first step, we are installing the xlrd module by using the pip command. We are installing the xlrd version as xlrd==1.2.0
Code:
pip install xlrd==1.2.0
Output:
2. After installing the modules, we are opening the python shell by using the python command to check xlrd package is installed on our system.
Code:
python
Output:
3. After login into the python shell in this step, we are checking xlrd package is installed in our windows system.
Code:
import xlrd
print (xlrd)
Output:
In the below example, we are installing it on the Ubuntu system as follows. The below steps shows how to install the xlrd module on the ubuntu system.
1. In the first step, we are installing the xlrd module by using the pip command. We are installing the xlrd version as 2.0.1.
Code:
pip install xlrd
Output:
2. After installing the xlrd modules we are opening the python shell by using the python3 command as follows.
Code:
python3
Output:
After login into the python shell in this step, we are checking xlrd package is installed in our Ubuntu system.
Code:
import xlrd
Output:
Python xlrd in Excel Files
We can also extract the data from an excel file by using python xlrd modules. The below example shows extracting the number of rows from the excel file as follows. In the below example, we are using the excel file name test.xlsx.
Code:
import xlrd
py_file = ("test.xlsx")
py_xlrd = xlrd.open_workbook (py_file)
py_sheet = py_xlrd.sheet_by_index (0)
py_sheet.cell_value (0, 0)
print (py_sheet.nrows)
Output:
The below example shows extract of the number of columns from the excel file as follows. In the below example, we are using the excel file name test.xlsx.
Code:
import xlrd
py_file = ("test.xlsx")
py_xlrd = xlrd.open_workbook (py_file)
py_sheet = py_xlrd.sheet_by_index (0)
py_sheet.cell_value (0, 0)
print (py_sheet.ncols)
Output:
The below example shows extract all column names from the excel file as follows.
Code:
import xlrd
py_file = ("test.xlsx")
py_xlrd = xlrd.open_workbook (py_file)
py_sheet = py_xlrd.sheet_by_index (0)
py_sheet.cell_value (0, 0)
for i in range (py_sheet.ncols):
print (py_sheet.cell_value(0, i))
Output:
The below example shows extract the first column from the excel file as follows.
Code:
import xlrd
py_file = ("test.xlsx")
py_xlrd = xlrd.open_workbook(py_file)
py_sheet = py_xlrd.sheet_by_index(0)
py_sheet.cell_value(0, 0)
for i in range(py_sheet.nrows):
print(py_sheet.cell_value(i, 0))
Output:
Conclusion
To use python xlrd we need to install xlrd package by using the pip command. Without installing the xlrd package we cannot use the same in our code. It retrieves data from a spreadsheet using the xlrd module. It is also used to read, write or modify data from excel files.
Recommended Article
This is a guide to Python xlrd. Here we discuss the definition, What is python xlrd, How to use python xlrd, examples along with code implementation and output. You may also have a look at the following articles to learn more –