Table
Table — Aspose.Cells FOSS for Python API Reference
aspose-cells-foss 通过以下方式表示 Excel 结构化表(ListObjects) Table 类以及 TableCollection 可访问为 ws.list_objects. 表格提供已命名的范围、自动筛选、标题行和内置于 XLSX 格式的分段行样式。.
Properties: aspose.cells_foss
from aspose.cells_foss import TableStyleInfo, TableColumnTableStyleInfo
描述应用于表格的视觉样式(交错行、首列/末列高亮)。.
Properties
TableStyleInfo()Properties
| Properties | Properties | Properties |
|---|---|---|
name | str | Excel 表格样式名称(例如,., "TableStyleMedium9"). |
show_first_column | bool | True 用于对第一列应用特殊格式。. |
show_last_column | bool | True 用于对最后一列应用特殊格式。. |
show_row_stripes | bool | True 用于应用交替行带状样式。. |
show_column_stripes | bool | True 用于应用交替列带状样式。. |
TableColumn
描述表格中的单个列。.
Properties
TableColumn(col_id: int, name: str)| Properties | Properties | Properties |
|---|---|---|
col_id | int | 1-based column identifier within the table (not the worksheet column index). |
name | str | 列标题文本。必须在表格内唯一。. |
Properties
| Properties | Properties | Properties |
|---|---|---|
id | int | 列标识符(只读)。. |
name | str | 列标题文本。. |
totals_row_function | str | 在合计行中显示的聚合函数:: 'none', 'sum', 'average', 'count', 'countNums', 'max', 'min', 'stdDev', 'var', 'custom'. |
totals_row_label | str | 在合计单元格中显示的标签文本,当 totals_row_function 为 'none'. |
totals_row_formula | str | 自定义公式在以下情况下使用 totals_row_function 是 'custom'. |
Properties
Table 对象不能直接构造。使用 ws.list_objects.add(...) 或 ws.list_objects.add_with_range(...).
Properties
| Properties | Properties | Properties |
|---|---|---|
name | str | 内部表名称(在工作簿中必须唯一,不能包含空格)。. |
display_name | str | 在 Excel 名称框中显示的名称。. |
ref | str | 表格覆盖的单元格范围(例如,., "A1:D10"). |
has_headers | bool | True 如果范围的第一行包含列标题。. |
show_totals_row | bool | True 在表格底部显示汇总合计行。. |
show_auto_filter | bool | True 在标题行显示自动筛选下拉箭头。. |
table_style_info | TableStyleInfo | 可视化样式设置(分条、突出显示列)。. |
columns | list[TableColumn] | 有序列表 TableColumn 对象(每个表列一个)。. |
TableCollection
TableCollection 可通过 ws.list_objects.。它管理工作表上的所有结构化表格。.
Properties
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在给定的单元格范围上创建表格。所有索引都是 0-based.。如果 name 是 None,,名称会自动生成(例如,., "Table1").
add_with_range
ws.list_objects.add_with_range(
cell_range: str,
name: str | None = None,
has_headers: bool = True
) -> Table从 A1 样式的范围字符串创建表(例如,., "A1:D10")。更易读于 add() 对于硬编码范围。.
属性和迭代
| Properties | Properties |
|---|---|
count | 此工作表上的表格数量(只读)。. |
__len__() | Properties count. |
__iter__() | 遍历所有 Table 对象。. |
__getitem__(index) | 返回 Table 在零基 index. |
Properties
下面的示例写入产品库存数据集,并将其转换为带有合计行的样式化 Excel 表格。.
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")