Parsování tabulek — Aspose.Note FOSS pro Python

Tabulky v dokumentech OneNote jsou vystaveny jako tříúrovňová hierarchie: Table → TableRow → TableCell. Každá buňka může obsahovat RichText, Image, a další uzly obsahu. Tato stránka pokrývá všechny vzory parsování tabulek podporované Aspose.Note FOSS pro Python.


Základní iterace tabulek

Získejte všechny tabulky v dokumentu a přečtěte text jejich buněk:

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

doc = Document("MyNotes.one")

for table_num, table in enumerate(doc.GetChildNodes(Table), start=1):
    print(f"\nTable {table_num}: {len(table.Columns)} column(s)")
    for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
        cells = row.GetChildNodes(TableCell)
        values = [
            " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            for cell in cells
        ]
        print(f"  Row {r}: {values}")

Vlastnosti tabulky

VlastnostTypPopis
Columnslist[TableColumn]Definice sloupců; každý TableColumn.Width (float) a .LockedWidth (bool)
IsBordersVisibleboolZda jsou zobrazeny okraje tabulky
Tagslist[NoteTag]Tagy OneNote připojené k tabulce
from aspose.note import Document, Table

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    print(f"Columns: {len(table.Columns)}")
    print(f"Widths (pts): {[col.Width for col in table.Columns]}")
    print(f"Borders visible: {table.IsBordersVisible}")

Exportovat tabulku do CSV

Převést tabulku do formátu CSV:

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

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

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([])  # blank line between tables

print(output.getvalue())

Extrahovat tabulky po stránkách

Omezte extrakci tabulek na jednotlivé stránky:

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

doc = Document("MyNotes.one")
for page_num, page in enumerate(doc.GetChildNodes(Page), start=1):
    tables = page.GetChildNodes(Table)
    if not tables:
        continue
    title = (
        page.Title.TitleText.Text
        if page.Title and page.Title.TitleText
        else f"Page {page_num}"
    )
    print(f"\n=== {title} ({len(tables)} table(s)) ===")
    for t, table in enumerate(tables, start=1):
        print(f"  Table {t}:")
        for row in table.GetChildNodes(TableRow):
            cells = row.GetChildNodes(TableCell)
            row_text = [
                " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
                for cell in cells
            ]
            print(f"    {row_text}")

Obsah buňky nad rámec prostého textu

Buňky tabulky mohou obsahovat Image a další CompositeNode obsah vedle 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 = [rt.Text for rt in cell.GetChildNodes(RichText) if rt.Text]
            images = cell.GetChildNodes(Image)
            print(f"  Cell texts: {texts}  images: {len(images)}")

Počítat a sumarizovat tabulky

Shromáždit statistiky o všech tabulkách v dokumentu:

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

doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Total tables: {len(tables)}")

for i, table in enumerate(tables, start=1):
    rows = table.GetChildNodes(TableRow)
    if rows:
        cols = len(rows[0].GetChildNodes(TableCell))
    else:
        cols = 0
    print(f"  Table {i}: {len(rows)} row(s) x {cols} column(s)  widths={[col.Width for col in table.Columns]}")

Prozkoumat značky na tabulkách

Tabulky podporují NoteTag položky přímo:

from aspose.note import Document, Table, TagStatus

doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
    for tag in table.Tags:
        is_completed = tag.Status == TagStatus.Completed
        print(f"Table tag: {tag.Label}  icon={tag.Icon}  completed={is_completed}")

Pozice tabulek v DOM

Tabulky se objevují jako potomci OutlineElement uzly v Outline kontejnery na každém Page. Hierarchie je:

Page
  └── Outline
        └── OutlineElement
              └── Table
                    └── TableRow
                          └── TableCell
                                └── RichText / Image

Tabulky můžete také získat přes GetChildNodes(Table) na libovolné úrovni předka uzlu a prohledává celý podstrom.


Tipy

  • table.Columns je seznam TableColumn objektů; použijte [col.Width for col in table.Columns] pro získání šířek sloupců v bodech. Délka se rovná počtu sloupců.
  • Obsah buňky není vždy čistě RichText; vždy zkontrolujte Image uzly také, pokud záleží na úplné věrnosti.
  • Použijte table.GetChildNodes(TableRow) místo iterování for row in table pokud potřebujete typovaný seznam místo obecného iterátoru.
  • IsBordersVisible odráží preferenci zobrazení uživatele OneNote v čase uložení; neovlivňuje extrakci obsahu.

Viz také

 Čeština