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, TableColumn

TableStyleInfo

Describes the visual style applied to a table (banding, first/last column highlights).

Constructor

TableStyleInfo()

Properties

PropertyTypeDescription
namestrExcel table style name (e.g., "TableStyleMedium9").
show_first_columnboolTrue to apply special formatting to the first column.
show_last_columnboolTrue to apply special formatting to the last column.
show_row_stripesboolTrue to apply alternating row banding.
show_column_stripesboolTrue to apply alternating column banding.

TableColumn

Describes a single column within a table.

Constructor

TableColumn(col_id: int, name: str)
ParameterTypeDescription
col_idint1-based column identifier within the table (not the worksheet column index).
namestrColumn header text. Must be unique within the table.

Properties

PropertyTypeDescription
idintColumn identifier (read-only).
namestrColumn header text.
totals_row_functionstrAggregate function shown in the totals row: 'none', 'sum', 'average', 'count', 'countNums', 'max', 'min', 'stdDev', 'var', 'custom'.
totals_row_labelstrLabel text shown in the totals cell when totals_row_function is 'none'.
totals_row_formulastrCustom 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

PropertyTypeDescription
namestrInternal table name (must be unique in the workbook, no spaces).
display_namestrDisplay name shown in the Excel Name Box.
refstrThe cell range covered by the table (e.g., "A1:D10").
has_headersboolTrue if the first row of the range contains column headers.
show_totals_rowboolTrue to display an aggregation totals row at the bottom of the table.
show_auto_filterboolTrue to show the auto-filter dropdown arrows in the header row.
table_style_infoTableStyleInfoVisual style settings (banding, highlight columns).
columnslist[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
) -> Table

Creates 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
) -> Table

Creates a table from an A1-style range string (e.g., "A1:D10"). More readable than add() for hardcoded ranges.

Properties and Iteration

MemberDescription
countNumber 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")

See Also