xlrd

raw JSON →
2.0.2 verified Tue May 12 auth: no python install: verified quickstart: verified maintenance

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.

pip install xlrd
error XLRDError: Excel 2007+ (xlsx) files are not supported
cause The user is attempting to open an Excel 2007+ (.xlsx) file with xlrd version 2.0.0 or later, which explicitly dropped support for these files.
fix
Use the openpyxl library to read and write .xlsx files, or downgrade xlrd to version 1.2.0 if legacy .xlsx support is essential (not recommended).
error XLRDError: Unsupported format, or corrupt file: Expected BOF record; found '...'
cause The file provided to `xlrd.open_workbook()` is not a valid Excel 97-2003 (.xls) file, is corrupt, or is a different file type (e.g., CSV, HTML) incorrectly named with a .xls extension.
fix
Ensure the file is a genuine and uncorrupted Excel 97-2003 (.xls) format file; use openpyxl for .xlsx files or pandas.read_csv() for CSVs.
error ModuleNotFoundError: No module named 'xlrd'
cause The `xlrd` library has not been installed in the Python environment where the code is being executed.
fix
Install the library using pip: pip install xlrd
error AttributeError: 'Book' object has no attribute 'save'
cause `xlrd` is a read-only library and does not provide methods to save or write changes to Excel files.
fix
Use the xlwt library for writing .xls files or the openpyxl library for writing .xlsx files.
breaking As of `xlrd` version 2.0.0, support for `.xlsx` files was entirely removed. Attempting to open an `.xlsx` file will result in an `xlrd.XLRDError: Excel xlsx file; not supported`.
fix For reading `.xlsx` files (Excel 2007+), use the `openpyxl` library instead. If using `pandas.read_excel`, ensure `openpyxl` is installed so pandas can automatically choose the correct engine for `.xlsx` files.
gotcha xlrd is strictly a *reader* library; it does not support writing to Excel files. For writing to `.xls` files, consider `xlwt`. For `.xlsx` files, `openpyxl` is the standard.
fix Use dedicated libraries like `xlwt` (for `.xls`) or `openpyxl` (for `.xlsx`) for writing Excel files.
gotcha Password-protected Excel files cannot be read by `xlrd`. The library does not provide functionality to decrypt or bypass password protection.
fix The file must be un-protected manually before `xlrd` can read it.
gotcha By default, `xlrd.open_workbook()` does *not* load formatting information (e.g., cell colors, fonts, borders) to save memory. Blank cells (cells with formatting but no data) are ignored.
fix To enable reading formatting information, pass `formatting_info=True` to `xlrd.open_workbook()`. Be aware that this can significantly increase memory usage, especially for large files. `book = xlrd.open_workbook('myfile.xls', formatting_info=True)`.
gotcha Many advanced Excel features are explicitly ignored or only partially supported. This includes Charts, Macros, Pictures, VBA modules, Comments, Hyperlinks, Autofilters, Pivot Tables, Conditional Formatting, and Data Validation. Formulas are read, but only their calculated results, not the formula text itself.
fix If these specific features are crucial, `xlrd` may not be the appropriate tool. Consider using COM automation (Windows-only) or commercial libraries that offer more comprehensive Excel interaction.
gotcha When using `pandas.read_excel` with `.xlsx` files, pandas typically tries `openpyxl` first. However, if `openpyxl` is not installed and `xlrd` (version >= 2.0.0) is the only Excel engine available, `pandas` will raise an error because `xlrd` no longer supports `.xlsx`.
fix Always install `openpyxl` (`pip install openpyxl`) when working with `.xlsx` files via `pandas.read_excel`. This ensures `pandas` uses the correct engine.
breaking The specified Excel file could not be found. This typically means the file does not exist at the given path, or the path is incorrect. `xlrd` cannot process files that are not accessible.
fix Ensure the Excel file exists in the expected location and the path provided to `xlrd.open_workbook()` is correct and accessible by the application. Double-check file name, extension, and directory.
breaking The specified Excel file was not found at the given path. This error occurs when the file either does not exist, is in a different location, or the application lacks the necessary permissions to access it.
fix Ensure the file exists at the specified path. Verify the file name and path for any typos. Confirm that the application has the correct read permissions for the file and its containing directory. If the file is expected to be generated, check upstream processes.
python os / libc status wheel install import disk
3.10 alpine (musl) wheel - 0.05s 18.4M
3.10 alpine (musl) - - 0.06s 18.4M
3.10 slim (glibc) wheel 1.5s 0.04s 19M
3.10 slim (glibc) - - 0.03s 19M
3.11 alpine (musl) wheel - 0.09s 20.3M
3.11 alpine (musl) - - 0.10s 20.3M
3.11 slim (glibc) wheel 1.6s 0.07s 21M
3.11 slim (glibc) - - 0.07s 21M
3.12 alpine (musl) wheel - 0.09s 12.2M
3.12 alpine (musl) - - 0.10s 12.2M
3.12 slim (glibc) wheel 1.5s 0.09s 13M
3.12 slim (glibc) - - 0.09s 13M
3.13 alpine (musl) wheel - 0.09s 11.9M
3.13 alpine (musl) - - 0.09s 11.8M
3.13 slim (glibc) wheel 1.5s 0.09s 12M
3.13 slim (glibc) - - 0.08s 12M
3.9 alpine (musl) wheel - 0.04s 17.9M
3.9 alpine (musl) - - 0.04s 17.9M
3.9 slim (glibc) wheel 1.8s 0.03s 18M
3.9 slim (glibc) - - 0.03s 18M

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}")