Open-Source Python Excel Reader Library
Free & Open Source Python library to read and extract data from Excel files (XLS, XLSX).
What is XLRD API?
XLRD is a lightweight and efficient open-source Python library designed for reading data from Excel files. It supports both the older XLS format and the newer XLSX format, making it a versatile tool for extracting data from spreadsheets. XLRD is particularly useful for developers who need to process Excel files programmatically, such as for data analysis, reporting, or automation tasks. While XLRD is primarily focused on reading Excel files, it can be combined with other libraries like XLWT or Openpyxl for editing and writing capabilities. Its simple and intuitive API makes it easy to integrate into Python applications.
XLRD API Features
Here are some of the key features of the XLRD API:
- Read Excel Files: XLRD can read both XLS and XLSX file formats, making it compatible with a wide range of Excel files.
- Extract Data: Easily extract data from cells, rows, and columns in Excel sheets.
- Sheet Management: Access and navigate through multiple sheets in an Excel workbook.
- Data Types: Supports various data types, including text, numbers, dates, and formulas.
- Lightweight: XLRD is a lightweight library with minimal dependencies, making it easy to install and use.
- Compatibility: Works seamlessly with other Python libraries like XLWT and Openpyxl for extended functionality.
Supported File Formats by XLRD
XLRD supports the following file formats:
File Format | Definition |
---|---|
XLS | Microsoft Excel 97-2003 spreadsheet file format |
XLSX | Microsoft Excel 2007 and above spreadsheet file format |
Getting Started with XLRD
You can install the XLRD library using pip. Here’s how to get started:
Installation
Install XLRD using the following pip command:
Installing XLRD
pip install xlrd
XLRD Python Code Examples
Here are some examples of how to use the XLRD library to read and extract data from Excel files.Example 1: Reading an Excel File
This example demonstrates how to open an Excel file and read data from a specific sheet.
Reading an Excel File with XLRD
import xlrd
# Open the Excel file
workbook = xlrd.open_workbook("example.xls")
# Select the first sheet
sheet = workbook.sheet_by_index(0)
# Read data from a specific cell
cell_value = sheet.cell_value(0, 0) # Row 0, Column 0
print("Cell Value:", cell_value)
Example 2: Iterating Through Rows
This example shows how to iterate through all rows in a sheet and print their values.
Iterating Through Rows
import xlrd
# Open the Excel file
workbook = xlrd.open_workbook("example.xls")
sheet = workbook.sheet_by_index(0)
# Iterate through all rows
for row_index in range(sheet.nrows):
row = sheet.row_values(row_index)
print(f"Row {row_index}: {row}")
Example 3: Extracting Data from Multiple Sheets
This example demonstrates how to extract data from multiple sheets in an Excel file.
Extracting Data from Multiple Sheets
import xlrd
# Open the Excel file
workbook = xlrd.open_workbook("example.xls")
# Iterate through all sheets
for sheet_name in workbook.sheet_names():
sheet = workbook.sheet_by_name(sheet_name)
print(f"Sheet Name: {sheet_name}")
# Print the first row of each sheet
first_row = sheet.row_values(0)
print("First Row:", first_row)
Resources
Conclusion
XLRD is a powerful and lightweight Python library for reading and extracting data from Excel files. Its simplicity and compatibility with both XLS and XLSX formats make it an excellent choice for developers working with Excel data. Whether you're analyzing data, automating reports, or integrating Excel file processing into your applications, XLRD provides the tools you need to get the job done efficiently. Combine it with other libraries like XLWT or Openpyxl for extended functionality, and you'll have a complete solution for working with Excel files in Python.
Similar Products
- Apache POI XWPF | Open Source Java API to Create & Modify DOCX files
- DocX | Open Source .NET API to Create & Modify DOCX files
- Docx4J | Open Source Java API to Create & Modify DOC and DOCX files
- ExcelDataReader | Open Source .NET API to read XLS, XLSX, CSV and Spreadsheet documents
- FileFormat.Cells | Cerate and Update Excel files with C# .NET