Aspose.Cells FOSS API Reference

Overview

Aspose.Cells FOSS is an open-source Python library for creating, reading, and modifying spreadsheet files without requiring Microsoft Excel. It supports XLSX, XLS, and CSV formats for input, and adds Markdown export via save_as_markdown(). The library is structured around a Workbook root object that manages worksheets, styles, charts, and I/O.

The core entry point is Workbook. Each Workbook contains one or more Worksheet objects accessed via wb.worksheets[index]. Individual cells are reached through the Cells collection using A1 notation or zero-based row/column indices.

from aspose.cells_foss import Workbook

wb = Workbook()
ws = wb.worksheets[0]
ws.cells["A1"].value = "Revenue"
ws.cells["B2"].value = 42
wb.save("output.xlsx")

Workbook and Worksheet

The Workbook class is the root object for all spreadsheet operations. It handles file loading, saving, and access to worksheets and document properties.

from aspose.cells_foss import Workbook

wb = Workbook()                      # new workbook
wb = Workbook("input.xlsx")          # open existing file
wb = Workbook("protected.xlsx", password="secret")  # open encrypted file

ws = wb.worksheets[0]                # first worksheet (0-based)
ws = wb.worksheets.add("Sheet2")     # add a named worksheet

ws.cells["A1"].put_value("Hello")    # write by A1 reference
ws.cells["A1"].value                 # read value (property, no parentheses)
ws.cells["A2"].formula = "=SUM(B1:B5)"

wb.save("output.xlsx")               # save as XLSX
wb.save("output.csv")                # auto-detects format from extension
wb.save_as_markdown("output.md")     # export as Markdown table
wb.save("output.xlsx", password="secret")  # save with AES encryption

Charts

Add charts to a worksheet using the add_* methods on ws.charts. Each method takes four positional arguments: top_row, left_col, bottom_row, right_col. Access the chart by index to set its title and data series.

from aspose.cells_foss import Workbook

wb = Workbook()
ws = wb.worksheets[0]
chart_idx = ws.charts.add_line(upper_left_row=0, upper_left_col=4,
                                lower_right_row=20, lower_right_col=12)
chart = ws.charts[chart_idx]
chart.title = "Monthly Sales"
chart.n_series.add("B2:B7", category_data="A2:A7", name="Sales")

# Other add methods: add_bar, add_pie, add_area, add_scatter,
# add_waterfall, add_combo, add_stock, add_surface, add_radar,
# add_treemap, add_sunburst, add_histogram, add_funnel,
# add_box_whisker, add_map

Shapes

Add drawing shapes and text boxes to a worksheet using ws.shapes.

from aspose.cells_foss import Workbook
from aspose.cells_foss import MsoDrawingType, TextAlignmentType, TextAnchorType

wb = Workbook()
ws = wb.worksheets[0]

shape = ws.shapes.add(MsoDrawingType.ROUNDED_RECTANGLE, 1, 1, 5, 5)
shape.text = "Hello"
shape.fill.fore_color = "90EE90"
shape.font.bold = True
shape.text_horizontal_alignment = TextAlignmentType.CENTER
shape.text_vertical_alignment = TextAnchorType.MIDDLE

textbox = ws.shapes.add_text_box(7, 1, 11, 8)
textbox.text = "Notes"

Cell Values and Styles

Read and write cell values using the .value property. Modify formatting by accessing cell.style directly.

from aspose.cells_foss import Workbook

workbook = Workbook()
worksheet = workbook.worksheets[0]

# Write values
worksheet.cells["A1"].value = "Hello"
worksheet.cells["B1"].value = "World"
worksheet.cells["A2"].value = 42
worksheet.cells["B2"].value = 3.14

# Read a value
value = worksheet.cells["A1"].value
print(f"Cell A1 contains: {value}")

# Apply a style via the cell's style property
cell = worksheet.cells["A1"]
cell.style.font.bold = True
cell.style.font.size = 14
cell.style.font.color = "FF0000"          # RRGGBB hex, no # prefix
cell.style.set_horizontal_alignment("center")
cell.style.set_number_format("#,##0.00")
cell.style.set_border("bottom", line_style="thin")

workbook.save("output.xlsx")

Note: when setting font.color, use a hex string without a # prefix (e.g., "FF0000" for red).

Data Validation

Add dropdown or other validation rules to a cell range using ws.data_validations.

from aspose.cells_foss import Workbook, DataValidationType

workbook = Workbook()
worksheet = workbook.worksheets[0]

validation = worksheet.data_validations.add("A1:A10")
validation.type = DataValidationType.LIST
validation.formula1 = '"Option1,Option2,Option3"'

workbook.save("validation.xlsx")

Encryption

Save or open password-protected workbooks:

from aspose.cells_foss import Workbook

workbook = Workbook()
worksheet = workbook.worksheets[0]
worksheet.cells["A1"].value = "Confidential Data"

# Save with password protection
workbook.save("protected.xlsx", password="mypassword")

# Open a password-protected file
workbook2 = Workbook("protected.xlsx", password="mypassword")

Public API

The Cells class provides access to individual cells using A1-style notation (e.g., "A1") or 1-based row/column tuple indices. The Cell class represents a single cell and exposes value, formula, and style as read/write properties. Merging and unmerging cells is supported via merge() and unmerge() methods on Cells.

from aspose.cells_foss import Workbook

workbook = Workbook()
worksheet = workbook.worksheets[0]
worksheet.cells["A1"].value = "Hello"
worksheet.cells["B1"].value = "World"    # A1 notation preferred
workbook.save("output.xlsx")
ClassPageDescription
WorkbookworkbookRoot workbook object
WorksheetworksheetSingle worksheet with cells, charts, shapes
CellscellsCell collection with A1 and 1-based (row, col) access
CellcellSingle cell: value, formula, style
StylestyleCell formatting: font, fill, borders, alignment
ChartchartEmbedded chart
DataValidationdata-validationCell range validation rule
CSVHandlercsv-handlerCSV import/export
MarkdownHandlermarkdown-handlerMarkdown export
JsonHandlerjson-handlerJSON export
ShapeshapeDrawing shape or text box
TabletableExcel structured table
SparklineGroupsparklineSparkline charts
HorizontalPageBreakCollectionpage-breakManual page breaks
AgileEncryptionParametersencryptionAES encryption parameters
SaveFormatworkbookOutput format enum

Common Patterns

# Styling a cell
cell = ws.cells["A1"]
cell.style.font.bold = True
cell.style.font.size = 14
cell.style.font.color = "FF0000"             # red (RRGGBB, no #)
cell.style.set_horizontal_alignment("center")
cell.style.set_number_format("#,##0.00")
cell.style.set_border("bottom", line_style="thin")
# Add a dropdown validation
from aspose.cells_foss import DataValidationType

dv = ws.data_validations.add("B1:B10")
dv.type = DataValidationType.LIST
dv.formula1 = '"Red,Green,Blue"'
dv.show_dropdown = True

See Also