CSVHandler — Aspose.Cells FOSS for Python API Reference

aspose-cells-foss provides CSV import and export through the CSVHandler class and two companion option classes. Module-level convenience functions are also available for simple use cases.

Package: aspose.cells_foss

from aspose.cells_foss import CSVHandler, CSVLoadOptions, CSVSaveOptions
from aspose.cells_foss import load_csv_workbook, save_workbook_as_csv

CSVLoadOptions

Options that control how a CSV file is parsed when loading.

Constructor

CSVLoadOptions()

Properties

PropertyTypeDefaultDescription
delimiterstr','Field separator character.
encodingstr'utf-8'Character encoding of the source file.
has_headerboolFalseWhen True, treats the first row as a header row.
quote_charstr'"'Character used to quote fields that contain the delimiter or newlines.
escape_charstr | NoneNoneOptional escape character (e.g., '\\').
skip_rowsint0Number of leading rows to skip before parsing data.
auto_detect_typesboolTrueWhen True, attempts to infer numeric and boolean types from cell text.
date_formatslist[]List of date format strings (e.g., ["%Y-%m-%d"]) tried in order during type detection.
true_valueslist[]Strings recognized as boolean True (e.g., ["yes", "true", "1"]).
false_valueslist[]Strings recognized as boolean False (e.g., ["no", "false", "0"]).

CSVSaveOptions

Options that control how a workbook is serialized to CSV.

Constructor

CSVSaveOptions()

Properties

PropertyTypeDefaultDescription
delimiterstr','Field separator written between values.
encodingstr'utf-8'Character encoding for the output file.
quote_charstr'"'Character used to quote fields containing the delimiter or newlines.
line_terminatorstr'\r\n'Line-ending sequence written after each row.
include_headerboolTrueWhen True, writes the header row.
worksheet_indexint0Zero-based index of the worksheet to export.
date_formatstrFormat string for datetime.date values (e.g., "%Y-%m-%d").
datetime_formatstrFormat string for datetime.datetime values.
time_formatstrFormat string for datetime.time values.
write_bomboolFalseWhen True, writes a UTF-8 BOM at the start of the file.

CSVHandler

CSVHandler contains only static methods and is never instantiated.

CSVHandler.save_csv

CSVHandler.save_csv(workbook, file_path: str, options: CSVSaveOptions | None = None) -> None

Saves the specified worksheet of workbook to a CSV file at file_path. If options is None, default CSVSaveOptions are used.

CSVHandler.save_csv_to_string

CSVHandler.save_csv_to_string(workbook, options: CSVSaveOptions | None = None) -> str

Serializes the worksheet to a CSV string and returns it. No file is written.

CSVHandler.load_csv

CSVHandler.load_csv(workbook, file_path: str, options: CSVLoadOptions | None = None) -> None

Parses the CSV file at file_path and loads its data into the first worksheet of workbook, replacing any existing content. If options is None, default CSVLoadOptions are used.

CSVHandler.load_csv_from_string

CSVHandler.load_csv_from_string(workbook, csv_content: str, options: CSVLoadOptions | None = None) -> None

Parses a CSV string and loads its data into the first worksheet of workbook.


Module Functions

load_csv_workbook

load_csv_workbook(file_path: str, options: CSVLoadOptions | None = None) -> Workbook

Creates a new Workbook, loads the CSV file into its first worksheet, and returns the workbook. Equivalent to creating a Workbook and calling CSVHandler.load_csv(wb, file_path, options).

save_workbook_as_csv

save_workbook_as_csv(workbook, file_path: str, options: CSVSaveOptions | None = None) -> None

Convenience wrapper around CSVHandler.save_csv(workbook, file_path, options).


Example

The following example loads a comma-delimited CSV file and saves it back with a semicolon delimiter.

from aspose.cells_foss import load_csv_workbook, CSVLoadOptions, CSVSaveOptions, save_workbook_as_csv

# Load the original CSV
load_opts = CSVLoadOptions()
load_opts.delimiter = ","
load_opts.has_header = True
load_opts.auto_detect_types = True

wb = load_csv_workbook("input.csv", options=load_opts)

# Inspect what was loaded
ws = wb.worksheets[0]
print(f"Rows loaded: {ws.cells.max_data_row + 1}")

# Save as semicolon-delimited CSV
save_opts = CSVSaveOptions()
save_opts.delimiter = ";"
save_opts.encoding = "utf-8"
save_opts.line_terminator = "\n"      # Unix line endings

save_workbook_as_csv(wb, "output_semicolon.csv", options=save_opts)

See Also