How to Load a Spreadsheet in Python

How to Load a Spreadsheet in Python

aspose-cells-foss lets you open existing spreadsheet files — XLSX, CSV, and other formats — in pure Python without Microsoft Office or native extensions. Pass the file path directly to Workbook() to load it, then navigate worksheets and read cell values through the Worksheet.cells grid.

Step-by-Step Guide

Step 1: Install the Package

Install from PyPI with pip:

pip install aspose-cells-foss>=26.3.1

Verify the install:

from aspose.cells_foss import Workbook
print("aspose-cells-foss is ready.")

Step 2: Import Required Classes

from aspose.cells_foss import Workbook

Step 3: Load a Workbook from a File

Pass the file path string to Workbook() to load an existing spreadsheet. The constructor accepts an optional password argument for protected files:

from aspose.cells_foss import Workbook

wb = Workbook("report.xlsx")
print("Loaded:", wb.file_path)

Step 4: Access a Worksheet

Use get_worksheet() with an integer index (0-based) or the sheet name string. Use the worksheets property to see how many sheets the workbook contains:

from aspose.cells_foss import Workbook

wb = Workbook("report.xlsx")

# By index
ws = wb.get_worksheet(0)

# By name
ws_named = wb.get_worksheet("Summary")

print(f"Worksheets: {len(wb.worksheets)}")
print(f"First sheet: {ws.name}")

Step 5: Read Cell Values

Access cells through the Worksheet.cells property. Use Cells.cell(row, col) to retrieve a Cell by its zero-based row and column coordinates. Read the data with the Cell.value property. Use Cell.is_empty() to skip blank cells safely:

from aspose.cells_foss import Workbook

wb = Workbook("report.xlsx")
ws = wb.get_worksheet(0)
cells = ws.cells

# Read individual cells
product = cells.cell(0, 0).value   # row 0, col 0 (A1)
revenue = cells.cell(0, 1).value   # row 0, col 1 (B1)

print(f"Product: {product}")
print(f"Revenue: {revenue}")

# Safe read with empty check
cell = cells.cell(1, 0)
if not cell.is_empty():
    print(f"A2: {cell.value}")

Step 6: Iterate All Rows

Use Cells.iter_rows() to walk through all rows without knowing dimensions. Pass values_only=True to receive plain Python values instead of Cell objects:

from aspose.cells_foss import Workbook

wb = Workbook("report.xlsx")
ws = wb.get_worksheet(0)

for row in ws.cells.iter_rows(0, None, 0, None, values_only=True):
    print(row)

Common Issues and Fixes

FileNotFoundError when loading Check that the path is correct and the file exists. Use pathlib.Path for portable paths: Workbook(str(Path("data") / "report.xlsx")).

Cell.value returns None The cell is empty or out of range. Use Cell.is_empty() to check before reading value. Out-of-range coordinates do not raise errors — they return empty cells.

Password-protected file raises an error Pass the password as the second argument: Workbook("protected.xlsx", "secret").

Only the first sheet loads when reading a CSV CSV files contain a single sheet. Use Workbook.load_csv(file_path) on an existing Workbook instance if you need to import CSV data into a specific sheet position rather than reading through the Workbook() constructor directly.

Sheet name not found with get_worksheet(name) Sheet names are case-sensitive. Print ws.name for each index in wb.worksheets to confirm the exact name before calling get_worksheet("Name").

Frequently Asked Questions

Does this work on Linux and macOS?

Yes. aspose-cells-foss is pure Python and runs on Windows, macOS, Linux, and Docker containers without any native extensions or system dependencies.

What file formats can I load?

The Workbook() constructor supports XLSX files. CSV files can be loaded by passing a .csv path or by calling Workbook.load_csv(file_path) on a Workbook instance.

How do I find the last row with data?

Iterate with Cells.iter_rows() and stop when all values in a row are None, or use Cell.is_empty() on boundary cells to detect where data ends.

Can I open a workbook without knowing its format in advance?

Pass the file path to Workbook(file_path). The library infers the format from the file extension and file headers. If the extension is missing or ambiguous, use an explicit save/load format option.

How do I read a cell by its letter address like “B3”?

Use zero-based row and column integers: B3 is row 2, col 1. Convert letter addresses with ord(col_letter) - ord("A") for single-letter columns. For example, B is ord("B") - ord("A") == 1, so cells.cell(2, 1) maps to B3.

See Also