Table, TableRow, TableCell — Aspose.Note FOSS for Python API Reference

类:Table

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

Table 表示一个表格,位于一个 OutlineElement.保存一个列表,包含 TableRow 子项,并公开列宽和边框可见性。.

Properties

PropertiesPropertiesPropertiesProperties
ColumnWidthslist[float]Properties每列的宽度(单位为点);长度等于列数
BordersVisibleboolProperties表格边框是否在 OneNote 中渲染
Tagslist[NoteTag]Properties附加到此表格的 OneNote 标签

继承自 CompositeNode / Node

FirstChild, LastChild, GetChildNodes(Type), for row in table, ParentNode, Document


类:TableRow

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

TableRow 是位于一个…中的单行 Table.它的直接子项是 TableCell 节点。.

Properties

rows = table.GetChildNodes(TableRow)
for row in rows:
    cells = row.GetChildNodes(TableCell)

类:TableCell

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

TableCell 是位于一个…中的单元格 TableRow.它可以包含 RichText, Image,,以及其他内容节点。.

Properties

for cell in row.GetChildNodes(TableCell):
    text = " ".join(rt.Text for rt in cell.GetChildNodes(RichText))

使用示例

读取所有表格:完整遍历

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

doc = Document("MyNotes.one")
for t_num, table in enumerate(doc.GetChildNodes(Table), start=1):
    print(f"\nTable {t_num}  cols={len(table.ColumnWidths)}  widths={table.ColumnWidths}")
    for r_num, row in enumerate(table.GetChildNodes(TableRow), start=1):
        cells = row.GetChildNodes(TableCell)
        row_data = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in cells
        ]
        print(f"  Row {r_num}: {row_data}")

将表格导出为 CSV

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

doc = Document("MyNotes.one")
buf = io.StringIO()
writer = csv.writer(buf)

for table in doc.GetChildNodes(Table):
    for row in table.GetChildNodes(TableRow):
        values = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in row.GetChildNodes(TableCell)
        ]
        writer.writerow(values)
    writer.writerow([])

csv_text = buf.getvalue()
print(csv_text)

统计行数和列数

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

doc = Document("MyNotes.one")
for i, table in enumerate(doc.GetChildNodes(Table), start=1):
    rows = table.GetChildNodes(TableRow)
    cols = len(rows[0].GetChildNodes(TableCell)) if rows else 0
    print(f"Table {i}: {len(rows)} row(s) x {cols} column(s)")

单元格中包含图像的表格

单元格可以包含 Image 节点,可与…并存或替代 RichText:

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

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    for row in table.GetChildNodes(TableRow):
        for cell in row.GetChildNodes(TableCell):
            texts = cell.GetChildNodes(RichText)
            images = cell.GetChildNodes(Image)
            if images:
                print(f"  Cell with {len(images)} image(s) and {len(texts)} text(s)")

检查表格的标签

from aspose.note import Document, Table

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    for tag in table.Tags:
        print(f"Table tag: {tag.label!r}  completed={tag.completed}")

DOM 位置

OutlineElement
  └── Table
        └── TableRow (0..n)
              └── TableCell (0..n)
                    ├── RichText (0..n)
                    └── Image (0..n)

Table 始终是…的子项 OutlineElement.使用 table.ParentNode 访问包含的 OutlineElement.


另见

 中文