xlrd

2.0.2 · maintenance · verified Sat Mar 28

xlrd is a Python library for reading data and formatting information from older Microsoft Excel `.xls` spreadsheet files (Excel 97-2003 format). As of version 2.0.0, it explicitly ceased support for `.xlsx` files due to security concerns and unreliability. The current version is 2.0.2, and it follows an infrequent, needs-based release cadence, primarily for maintenance of `.xls` support.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to open an existing `.xls` workbook, access its sheets, and read cell values. Remember, `xlrd` only supports the `.xls` format (Excel 97-2003) as of version 2.0.0. For newer `.xlsx` files, consider `openpyxl`.

import xlrd
import os

# --- IMPORTANT NOTE ---
# xlrd *only* reads .xls files (Excel 97-2003 format) as of version 2.0.0.
# This example assumes 'example.xls' exists and is a valid .xls file.
# For .xlsx files (Excel 2007+), use a library like 'openpyxl'.
# ----------------------

file_path = "example.xls" # Replace with your .xls file path

try:
    # Open the workbook
    book = xlrd.open_workbook(file_path)

    print(f"Successfully opened '{file_path}'.")

    # Print number of sheets
    print(f"The number of worksheets is: {book.nsheets}")

    # Print sheet names
    print(f"Worksheet name(s): {book.sheet_names()}")

    # Get the first sheet by index (0-indexed)
    sh = book.sheet_by_index(0)

    # Print sheet details
    print(f"\nSheet '{sh.name}' details:")
    print(f"  Rows: {sh.nrows}")
    print(f"  Columns: {sh.ncols}")

    # Read a cell value (e.g., cell at row index 0, column index 0)
    if sh.nrows > 0 and sh.ncols > 0:
        cell_value = sh.cell_value(rowx=0, colx=0)
        print(f"  Value of cell (0,0): {cell_value}")
    else:
        print("  Sheet is empty or does not have cell (0,0).")

    # Iterate over rows and print values (first 3 rows for brevity)
    print("\nFirst 3 rows of data (or fewer if sheet has less):")
    for rx in range(min(sh.nrows, 3)):
        print(f"  Row {rx}: {sh.row_values(rx)}")

except FileNotFoundError:
    print(f"Error: The file '{file_path}' was not found.")
    print("Please ensure a valid 'example.xls' file exists in the specified path.")
except xlrd.XLRDError as e:
    print(f"Error opening Excel file: {e}")
    print("Please ensure the file is a valid .xls (Excel 97-2003) format.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →