Analisi delle tabelle — Aspose.Note FOSS per Python
Le tabelle nei documenti OneNote sono esposte come una gerarchia a tre livelli: Table → TableRow → TableCell. Ogni cella può contenere RichText, Image, e altri nodi di contenuto. Questa pagina copre tutti i modelli di analisi delle tabelle supportati da Aspose.Note FOSS per Python.
Iterazione di base delle tabelle
Recupera tutte le tabelle nel documento e leggi il testo delle loro celle:
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}")Proprietà della tabella
| Proprietà | Tipo | Descrizione |
|---|---|---|
Columns | list[TableColumn] | Definizioni di colonna; ogni TableColumn ha .Width (float) e .LockedWidth (bool) |
IsBordersVisible | bool | Se i bordi della tabella sono visualizzati |
Tags | list[NoteTag] | Tag OneNote allegati alla tabella |
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}")Esporta la tabella in CSV
Converti una tabella in formato 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())Estrai tabelle per pagina
Limita l’estrazione delle tabelle a pagine individuali:
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}")Contenuto della cella oltre il testo semplice
Le celle della tabella possono contenere Image e altri CompositeNode contenuti accanto a 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)}")Conta e riepiloga le tabelle
Raccogli statistiche su tutte le tabelle in un documento:
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]}")Ispeziona i tag sulle tabelle
Le tabelle supportano NoteTag elementi direttamente:
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}")Posizione DOM delle Tabelle
Le tabelle appaiono come figli di OutlineElement nodi all’interno di Outline contenitori su ciascuno Page. La gerarchia è:
Page
└── Outline
└── OutlineElement
└── Table
└── TableRow
└── TableCell
└── RichText / ImagePuoi anche accedere alle tabelle tramite GetChildNodes(Table) a qualsiasi livello di nodo antenato, e ricerca l’intero sottoalbero.
Suggerimenti
table.Columnsè un elenco diTableColumnoggetti; usa[col.Width for col in table.Columns]per ottenere le larghezze delle colonne in punti. La lunghezza è pari al numero di colonne.- Il contenuto della cella non è sempre puramente
RichText; verifica sempre la presenza diImagenodi anche se la fedeltà completa è importante. - Usa
table.GetChildNodes(TableRow)invece di iterarefor row in tablese hai bisogno di un elenco tipizzato invece di un iteratore generico. IsBordersVisibleriflette la preferenza di visualizzazione dell’utente OneNote al momento del salvataggio; non influisce sull’estrazione del contenuto.