Typing stubs for xlrd

2.0.0.20260408 · active · verified Mon Apr 13

types-xlrd provides static type checking stubs for the `xlrd` library. `xlrd` is a Python library designed for reading data and formatting information from older Microsoft Excel `.xls` files (Excel 97-2003 Workbook format). The core `xlrd` library is currently at version 2.0.2 and has a stable release cadence, primarily focusing on bug fixes and maintaining `.xls` parsing functionality. The `types-xlrd` package aligns with the `xlrd` library's API for type hinting purposes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open an Excel `.xls` file, access a specific sheet, read individual cell values, and iterate through all cells in the sheet using `xlrd`. It includes basic error handling for file not found or incorrect file format. Users must provide an existing `.xls` file.

import xlrd
import os

# Create a dummy .xls file for demonstration if it doesn't exist
# In a real scenario, you would open an existing .xls file
xls_file_path = 'example.xls'
if not os.path.exists(xls_file_path):
    # This part requires an actual .xls file or a library to create one (e.g., xlwt)
    # For a runnable example without xlwt, we'll simulate opening.
    # In practice, ensure 'example.xls' is a valid Excel 97-2003 file.
    print(f"Please create a dummy Excel 97-2003 (.xls) file named '{xls_file_path}' with some data.")
    print("For example, put 'Hello' in A1 and 'World' in B1 of Sheet1.")
    # For this quickstart, we'll proceed assuming the file exists
    # and will raise an error if not, which is expected for `xlrd`.

try:
    # Open the workbook (assumes example.xls exists and is an .xls file)
    book: xlrd.Book = xlrd.open_workbook(xls_file_path)

    # Get the first sheet by index
    sheet: xlrd.sheet.Sheet = book.sheet_by_index(0)

    # Read a cell value (e.g., A1, which is (0, 0))
    cell_value: str = sheet.cell_value(0, 0)
    print(f"Value from cell A1: {cell_value}")

    # Iterate through rows and print values
    print("\nAll values in the first sheet:")
    for row_idx in range(sheet.nrows):
        for col_idx in range(sheet.ncols):
            cell = sheet.cell(row_idx, col_idx)
            print(f"({row_idx}, {col_idx}): {cell.value}", end="\t")
        print()

except xlrd.biffh.XLRDError as e:
    print(f"Error opening Excel file: {e}")
    print("Make sure 'example.xls' is a valid Excel 97-2003 (.xls) file.")
except FileNotFoundError:
    print(f"Error: The file '{xls_file_path}' was not found. Please create it.")

view raw JSON →