Last modified: Nov 19, 2025 By Alexander Williams
Read Specific Rows and Columns with Python xlrd
Working with Excel files is common in data analysis. Often you need specific data. The xlrd library helps with this task. It lets you read Excel files in Python.
This guide shows how to extract specific rows and columns. You will learn practical techniques. These methods save time and memory.
Installing xlrd Library
First, ensure xlrd is installed. Use pip for installation. Our guide on install xlrd and xlwt in Python covers this in detail.
Run this command in your terminal:
pip install xlrd
Loading Excel Workbooks
Start by loading your Excel file. Use the open_workbook function. This creates a workbook object.
For more on workbook handling, see our Python xlrd guide on loading workbooks.
import xlrd
# Open the Excel file
workbook = xlrd.open_workbook('sample_data.xlsx')
Accessing Specific Sheets
Excel files contain multiple sheets. You can access them by name or index. Use the sheet_by_name method.
# Access sheet by name
sheet = workbook.sheet_by_name('Sheet1')
# Or access by index (0-based)
sheet = workbook.sheet_by_index(0)
Reading Specific Rows
To read a specific row, use row_values. Provide the row index. Remember indexing starts at 0.
# Read row at index 2 (third row)
row_data = sheet.row_values(2)
print("Row 3 data:", row_data)
Row 3 data: ['Product C', 75.0, 12.0, 900.0]
Reading Specific Columns
Similarly, use col_values for columns. Provide the column index. This returns all values in that column.
# Read column at index 1 (second column)
column_data = sheet.col_values(1)
print("Column 2 data:", column_data)
Column 2 data: ['Price', 100.0, 150.0, 75.0]
Reading Cell Values Directly
For single cells, use cell_value. Specify row and column indices. This is efficient for specific data points.
# Read cell at row 1, column 2
cell_value = sheet.cell_value(1, 2)
print("Cell value:", cell_value)
Cell value: 10.0
Reading Multiple Specific Rows
Often you need multiple rows. Loop through desired indices. Use the row method for each.
# Read rows 1 to 3 (indices 1, 2, 3)
print("Multiple rows:")
for row_idx in [1, 2, 3]:
row_data = sheet.row_values(row_idx)
print(f"Row {row_idx + 1}: {row_data}")
Multiple rows:
Row 2: ['Product A', 100.0, 10.0, 1000.0]
Row 3: ['Product B', 150.0, 8.0, 1200.0]
Row 4: ['Product C', 75.0, 12.0, 900.0]
Reading Multiple Specific Columns
Similarly, extract multiple columns. Loop through column indices. Use col_values for each.
# Read columns 0 and 2 (first and third)
print("Multiple columns:")
for col_idx in [0, 2]:
col_data = sheet.col_values(col_idx)
print(f"Column {col_idx + 1}: {col_data}")
Multiple columns:
Column 1: ['Product Name', 'Product A', 'Product B', 'Product C']
Column 3: ['Quantity', 10.0, 8.0, 12.0]
Reading Row and Column Ranges
You can read contiguous ranges. Use slicing with row_values. Specify start and end indices.
# Read rows 1 to 3 (exclusive of row 4)
print("Row range:")
for row_idx in range(1, 4):
row_data = sheet.row_values(row_idx)
print(f"Row {row_idx + 1}: {row_data}")
Row range:
Row 2: ['Product A', 100.0, 10.0, 1000.0]
Row 3: ['Product B', 150.0, 8.0, 1200.0]
Row 4: ['Product C', 75.0, 12.0, 900.0]
Handling Different Data Types
Excel cells contain various data types. xlrd automatically converts them. Numbers become floats, text becomes strings.
# Check data types in a row
row_data = sheet.row_values(1)
print("Data types:")
for i, value in enumerate(row_data):
print(f"Column {i}: {value} (type: {type(value).__name__})")
Data types:
Column 0: Product A (type: str)
Column 1: 100.0 (type: float)
Column 2: 10.0 (type: float)
Column 3: 1000.0 (type: float)
Practical Example: Sales Data Analysis
Let's analyze sample sales data. We'll extract specific information. This shows real-world application.
# Calculate total revenue from specific products
total_revenue = 0
product_rows = [1, 2] # Rows for Product A and B
for row_idx in product_rows:
price = sheet.cell_value(row_idx, 1) # Price column
quantity = sheet.cell_value(row_idx, 2) # Quantity column
revenue = price * quantity
total_revenue += revenue
product_name = sheet.cell_value(row_idx, 0)
print(f"{product_name} revenue: ${revenue}")
print(f"Total revenue for selected products: ${total_revenue}")
Product A revenue: $1000.0
Product B revenue: $1200.0
Total revenue for selected products: $2200.0
Error Handling
Always handle potential errors. Files might be missing. Indices might be invalid. Use try-except blocks.
try:
# Attempt to read non-existent row
row_data = sheet.row_values(100)
print("Row data:", row_data)
except IndexError:
print("Error: Row index out of range")
try:
# Attempt to read non-existent column
col_data = sheet.col_values(100)
print("Column data:", col_data)
except IndexError:
print("Error: Column index out of range")
Error: Row index out of range
Error: Column index out of range
Performance Considerations
Reading specific data is efficient. It avoids loading entire files. This saves memory with large datasets.
For very large files, consider reading in chunks. Process only needed data. This optimizes performance.
When to Use xlrd vs Other Libraries
xlrd is great for reading .xls files. For .xlsx files, consider openpyxl. Choose based on your file format.
If you need to switch libraries, see our guide on how to uninstall xlrd in Python.
Conclusion
Reading specific rows and columns is essential. The xlrd library makes this straightforward. You can extract exactly what you need.
Remember the key methods: row_values, col_values, and cell_value. Use them to target specific data. This approach is efficient and practical.
For beginners, start with our Python xlrd tutorial for reading Excel files. It provides a solid foundation for Excel manipulation in Python.