TableRow / TableCell

TableRow / TableCell — Aspose.Note FOSS for Python API Reference

类:TableRow

Properties: aspose.note Properties: from aspose.note import TableRow Properties: CompositeNode

TableRow 表示单行在 Table. 它的直接子节点是 TableCell 节点。.


Properties

TableRow 没有超出从 CompositeNodeNode.

继承自 CompositeNode

属性 / 方法Properties
FirstChild第一个直接子节点 TableCell, 或 None
LastChild最后一个直接子节点 TableCell, 或 None
AppendChildLast(node)添加一个 TableCell
AppendChildFirst(node)前置一个 TableCell
InsertChild(index, node)插入一个 TableCell 在特定位置
RemoveChild(node)删除一个 TableCell
GetChildNodes(Type)递归深度优先搜索
for cell in row直接迭代 TableCell 子项

继承自 Node

PropertiesProperties
ParentNode包含的 Table
DocumentDocument

类:TableCell

Properties: aspose.note Properties: from aspose.note import TableCell Properties: CompositeNode

TableCell 表示单个单元格,位于 TableRow.它可以包含 RichText, Image,,以及其他内容节点作为子节点。.


Properties

TableCell 没有超出从…继承的属性 CompositeNode 以及 Node.

继承自 CompositeNode

属性 / 方法Properties
FirstChild第一个直接内容子节点
LastChild最后一个直接内容子节点
AppendChildLast(node)向此单元格追加内容节点
AppendChildFirst(node)在前面添加内容节点
InsertChild(index, node)在特定位置插入
RemoveChild(node)移除内容节点
GetChildNodes(Type)递归深度优先搜索
for child in cell遍历直接子节点

继承自 Node

PropertiesProperties
ParentNode包含的 TableRow
DocumentDocument

使用示例

遍历所有表格行和单元格

from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    print(f"Table column widths: {[col.Width for col in table.Columns]}")
    for row in table.GetChildNodes(TableRow):
        row_texts = []
        for cell in row.GetChildNodes(TableCell):
            texts = cell.GetChildNodes(RichText)
            cell_text = " ".join(rt.Text for rt in texts if rt.Text)
            row_texts.append(cell_text)
        print("  |", " | ".join(row_texts), "|")

将每个单元格的文本提取为扁平列表

from aspose.note import Document, TableCell, RichText

doc = Document("MyNotes.one")
for cell in doc.GetChildNodes(TableCell):
    for rt in cell.GetChildNodes(RichText):
        if rt.Text.strip():
            print(rt.Text)

以编程方式构建表格

from aspose.note import (
    Document, Page, Outline, OutlineElement,
    Table, TableRow, TableCell, RichText, SaveFormat
)

doc = Document()
page = doc.AppendChildLast(Page())
outline = page.AppendChildLast(Outline())
elem = outline.AppendChildLast(OutlineElement())

from aspose.note import TableColumn
table = elem.AppendChildLast(Table(
    IsBordersVisible=True,
    Columns=[TableColumn(Width=100.0), TableColumn(Width=200.0)],
))

headers = ["Name", "Value"]
row = table.AppendChildLast(TableRow())
for header_text in headers:
    cell = row.AppendChildLast(TableCell())
    rt = cell.AppendChildLast(RichText())
    rt.Append(header_text)

data_row = table.AppendChildLast(TableRow())
for value in ["Item A", "42"]:
    cell = data_row.AppendChildLast(TableCell())
    rt = cell.AppendChildLast(RichText())
    rt.Append(value)

doc.Save("table-output.pdf", SaveFormat.Pdf)

统计行数和单元格数

from aspose.note import Document, Table, TableRow, TableCell

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    rows = table.GetChildNodes(TableRow)
    cells = table.GetChildNodes(TableCell)
    print(f"Rows: {len(rows)}  Cells: {len(cells)}")

None 安全性

TableRowTableCell 没有可为空的自有属性。返回的子列表 GetChildNodes 始终是列表(可能为空)。.


另见

 中文