Tabellparsing — Aspose.Note FOSS för Python
Tabeller i OneNote-dokument exponeras som en hierarki med tre nivåer: Table → TableRow → TableCell. Varje cell kan innehålla RichText, Image, och andra innehållsnoder. Den här sidan täcker alla tabellparsningsmönster som stöds av Aspose.Note FOSS för Python.
Grundläggande tabelliteration
Hämta alla tabeller i dokumentet och läs deras celltext:
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}")Tabellegenskaper
| Egenskap | Typ | Beskrivning |
|---|---|---|
Columns | list[TableColumn] | Kolumndefinitioner; varje TableColumn har .Width (float) och .LockedWidth (bool) |
IsBordersVisible | bool | Om tabellramar visas |
Tags | list[NoteTag] | OneNote-taggar som är bifogade till tabellen |
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}")Exportera tabell till CSV
Konvertera en tabell till CSV-format:
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())Extrahera tabeller per sida
Begränsa tabellutdrag till enskilda sidor:
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}")Cellinnehåll utöver vanlig text
Tabellceller kan innehålla Image och andra CompositeNode innehåll tillsammans med 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)}")Räkna och sammanfatta tabeller
Samla statistik om alla tabeller i ett dokument:
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]}")Inspektera taggar på tabeller
Tabeller stöder NoteTag objekt direkt:
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}")DOM-position för tabeller
Tabeller visas som barn till OutlineElement noder inom Outline behållare på varje Page. Hierarkin är:
Page
└── Outline
└── OutlineElement
└── Table
└── TableRow
└── TableCell
└── RichText / ImageDu kan också nå tabeller via GetChildNodes(Table) på vilken förfadernodnivå som helst, och den söker i hela underträdet.
Tips
table.Columnsär en lista medTableColumnobjekt; använd[col.Width for col in table.Columns]för att få kolumnbredder i punkter. Längden är lika med antalet kolumner.- Cellinnehåll är inte alltid rent
RichText; kontrollera alltid förImagenoder också om fullständig trohet är viktig. - Använd
table.GetChildNodes(TableRow)istället för att itererafor row in tableom du behöver en typad lista istället för en generisk iterator. IsBordersVisibleåterspeglar OneNote-användarens visningspreferens vid sparning; den påverkar inte extrahering av innehåll.