excelrd

3.0.0 · active · verified Thu Apr 16

excelrd is a Python library designed for developers to extract data and formatting information from Microsoft Excel spreadsheet files, supporting both the older `.xls` and newer `.xlsx` formats. It is a modified version of the `xlrd` library, specifically updated to maintain compatibility and functionality with modern Python versions (3.7+), as `xlrd` itself no longer supports `.xlsx` files in its recent versions. The library is currently at version 3.0.0 and sees releases primarily for Python compatibility and maintenance.

Common errors

Warnings

Install

Imports

Quickstart

The quickstart demonstrates how to open an Excel workbook, retrieve the number of sheets and their names, access a specific sheet by its index, and then iterate through its cells to print their values. This pattern is fundamental for extracting tabular data.

import excelrd
import os

# Create a dummy Excel file for demonstration (replace with your actual file)
excel_file_path = "namesdemo.xls"
# In a real scenario, you'd have an existing .xls or .xlsx file.
# For a runnable example, we'll simulate opening one.
# Assuming 'namesdemo.xls' exists with at least 3 sheets.
# In a real app, ensure the file exists.

# Example of opening and reading data from an Excel file
try:
    book = excelrd.open_workbook(excel_file_path)
    print(f"The number of worksheets is {book.nsheets}")
    print(f"Worksheet name(s): {', '.join(book.sheet_names())}")

    # Access a specific sheet by index (e.g., the first sheet)
    sh = book.sheet_by_index(0)
    print(f"{sh.name}: rows={sh.nrows}, cols={sh.ncols}")

    # Iterate through rows and print cell values
    for row_idx in range(sh.nrows):
        for col_idx in range(sh.ncols):
            cell = sh.cell(row_idx, col_idx)
            if cell.value is not None and str(cell.value).strip(): # Check for non-empty cells
                print(f"row={row_idx}, col={col_idx}, value={cell.value}")

except FileNotFoundError:
    print(f"Error: Excel file '{excel_file_path}' not found. Please create it or provide a valid path.")
except Exception as e:
    print(f"An error occurred: {e}")

view raw JSON →