Table — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss represents Excel structured tables (ListObjects) through the Table class and the TableCollection accessible as ws.list_objects. Tables provide named ranges, auto-filter, header rows, and banded-row styling built into the XLSX format.
Package: aspose.cells_foss
from aspose.cells_foss import TableStyleInfo, TableColumnTableStyleInfo
Describes the visual style applied to a table (banding, first/last column highlights).
Constructor
TableStyleInfo()Properties
| Property | Type | Description |
|---|---|---|
name | str | Excel table style name (e.g., "TableStyleMedium9"). |
show_first_column | bool | True to apply special formatting to the first column. |
show_last_column | bool | True to apply special formatting to the last column. |
show_row_stripes | bool | True to apply alternating row banding. |
show_column_stripes | bool | True to apply alternating column banding. |
TableColumn
Describes a single column within a table.
Constructor
TableColumn(col_id: int, name: str)| Parameter | Type | Description |
|---|---|---|
col_id | int | 1-based column identifier within the table (not the worksheet column index). |
name | str | Column header text. Must be unique within the table. |
Properties
| Property | Type | Description |
|---|---|---|
id | int | Column identifier (read-only). |
name | str | Column header text. |
totals_row_function | str | Aggregate function shown in the totals row: 'none', 'sum', 'average', 'count', 'countNums', 'max', 'min', 'stdDev', 'var', 'custom'. |
totals_row_label | str | Label text shown in the totals cell when totals_row_function is 'none'. |
totals_row_formula | str | Custom formula used when totals_row_function is 'custom'. |
Table
Table objects are not constructed directly. Use ws.list_objects.add(...) or ws.list_objects.add_with_range(...).
Properties
| Property | Type | Description |
|---|---|---|
name | str | Internal table name (must be unique in the workbook, no spaces). |
display_name | str | Display name shown in the Excel Name Box. |
ref | str | The cell range covered by the table (e.g., "A1:D10"). |
has_headers | bool | True if the first row of the range contains column headers. |
show_totals_row | bool | True to display an aggregation totals row at the bottom of the table. |
show_auto_filter | bool | True to show the auto-filter dropdown arrows in the header row. |
table_style_info | TableStyleInfo | Visual style settings (banding, highlight columns). |
columns | list[TableColumn] | Ordered list of TableColumn objects (one per table column). |
TableCollection
TableCollection is accessed as ws.list_objects. It manages all structured tables on a worksheet.
Methods
add
ws.list_objects.add(
start_row: int,
start_col: int,
end_row: int,
end_col: int,
has_headers: bool = True,
name: str | None = None
) -> TableCreates a table over the given cell range. All indices are 0-based. If name is None, a name is generated automatically (e.g., "Table1").
add_with_range
ws.list_objects.add_with_range(
cell_range: str,
name: str | None = None,
has_headers: bool = True
) -> TableCreates a table from an A1-style range string (e.g., "A1:D10"). More readable than add() for hardcoded ranges.
Properties and Iteration
| Member | Description |
|---|---|
count | Number of tables on this worksheet (read-only). |
__len__() | Returns count. |
__iter__() | Iterates over all Table objects. |
__getitem__(index) | Returns the Table at zero-based index. |
Example
The following example writes a product inventory dataset and converts it into a styled Excel table with a totals row.
from aspose.cells_foss import Workbook
wb = Workbook()
ws = wb.worksheets[0]
ws.name = "Inventory"
# Write header and data rows
headers = ["SKU", "Product", "Qty", "Unit Price"]
rows = [
["A001", "Widget Alpha", 150, 12.99],
["A002", "Widget Beta", 80, 24.50],
["A003", "Gadget Gamma", 45, 49.99],
["A004", "Gadget Delta", 210, 8.75],
]
for col, h in enumerate(headers):
ws.cells[0][col].put_value(h)
for row_i, row in enumerate(rows, start=1):
for col_i, val in enumerate(row):
ws.cells[row_i][col_i].put_value(val)
# Create a table over A1:D5
table = ws.list_objects.add_with_range("A1:D5", name="InventoryTable")
# Configure style
table.table_style_info.name = "TableStyleMedium9"
table.table_style_info.show_row_stripes = True
table.table_style_info.show_first_column = False
table.table_style_info.show_last_column = False
# Add a totals row with sum on Qty and Unit Price columns
table.show_totals_row = True
table.columns[2].totals_row_function = "sum" # Qty
table.columns[3].totals_row_function = "sum" # Unit Price
table.columns[0].totals_row_label = "Total"
wb.save("inventory_table.xlsx")