How to Load Files with Aspose.Cells FOSS

How to Load Files with Aspose.Cells FOSS

Problem

Load spreadsheet files (XLSX, XLS, CSV) into Aspose.Cells FOSS for programmatic manipulation. The Workbook class accepts a file path or a file-like stream and returns an in-memory workbook ready for reading or editing.

from aspose.cells_foss import Workbook

# Load an XLSX file
workbook = Workbook("input.xlsx")
worksheet = workbook.worksheets[0]

Prerequisites

To load files using Aspose.Cells FOSS in Python, ensure you have Python 3.7 or later installed. Install the library using pip with the command pip install aspose-cells-foss>=26.3.1. After installation, import the library using from aspose.cells_foss import Workbook.

  • Python 3.7 or later
  • pip package manager
  • aspose-cells-foss>=26.3.1 installed via pip
  • Basic understanding of Python file handling

Loading the File

Aspose.Cells FOSS loads spreadsheet data from file paths or streams using the Workbook class. Pass a local file path directly to the constructor, or provide a file-like object (e.g., io.BytesIO) for in-memory loading. To load CSV files, use the load_csv_workbook() convenience function.

from aspose.cells_foss import Workbook

# Load from file path (XLSX or XLS)
workbook = Workbook("data.xlsx")

# Load from stream (e.g., BytesIO)
import io
with open("data.xlsx", "rb") as f:
    stream = io.BytesIO(f.read())
    workbook = Workbook(stream)

When loading CSV files, use the load_csv_workbook() function which creates and returns a new Workbook populated with the CSV data.

Code Example

This example demonstrates loading an XLSX file and a CSV file, then reading a cell value from each.

from aspose.cells_foss import Workbook, load_csv_workbook

# Load an XLSX workbook from a file path
workbook = Workbook("input.xlsx")
worksheet = workbook.worksheets[0]

# Read a cell value using the .value property
val = worksheet.cells["A1"].value
print(f"A1 value: {val}")

# Load a CSV file using load_csv_workbook
csv_workbook = load_csv_workbook("data.csv")
csv_worksheet = csv_workbook.worksheets[0]

# Read from the CSV-sourced workbook
csv_val = csv_worksheet.cells["A1"].value
print(f"CSV A1 value: {csv_val}")

Supported Formats

Aspose.Cells FOSS supports loading files in several common spreadsheet and data interchange formats. All supported formats can be loaded via the Workbook class constructor.

FormatExtensionNotes
Excel 2007–2019.xlsxStandard Office Open XML format
Excel 97–2003.xlsBinary BIFF format
CSV.csvComma-separated values; use load_csv_workbook("file.csv")

See Also