Analizza le tabelle di OneNote in Python
Microsoft OneNote consente agli utenti di incorporare tabelle strutturate direttamente nelle pagine, perfette per elenchi di attività, programmazioni, matrici di confronto e moduli di raccolta dati. Aspose.Nota FOSS per Python rende possibile estrarre tutti questi dati tabulari in modo programmatico, senza la necessità di installare Microsoft Office.
Installa
pip install aspose-note
Carica il documento e trova le tabelle
GetChildNodes(Table) esegue una ricerca ricorsiva su tutto il documento e restituisce ogni tabella come un Table oggetto:
from aspose.note import Document, Table
doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")
Leggi i valori delle celle
Le tabelle seguono una gerarchia a tre livelli: Table → TableRow → TableCell. Ogni cella contiene RichText nodi i cui .Text fornisce il contenuto in testo semplice:
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}:")
for r_num, row in enumerate(table.GetChildNodes(TableRow), start=1):
cells = row.GetChildNodes(TableCell)
row_values = [
" ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
for cell in cells
]
print(f" Row {r_num}: {row_values}")
Ispeziona le larghezze delle colonne
Table.ColumnWidths restituisce la larghezza memorizzata di ogni colonna in punti:
from aspose.note import Document, Table
doc = Document("MyNotes.one")
for i, table in enumerate(doc.GetChildNodes(Table), start=1):
print(f"Table {i}: {len(table.ColumnWidths)} column(s)")
print(f" Widths (pts): {table.ColumnWidths}")
print(f" Borders visible: {table.BordersVisible}")
Esporta tutte le tabelle in CSV
Converti ogni tabella del documento in formato CSV:
import csv, 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 row between tables
with open("tables.csv", "w", encoding="utf-8", newline="") as f:
f.write(output.getvalue())
print("Saved tables.csv")
Esporta le tabelle in un dizionario Python / JSON
import json
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
result = []
for table in doc.GetChildNodes(Table):
rows = []
for row in table.GetChildNodes(TableRow):
cells = [
" ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
for cell in row.GetChildNodes(TableCell)
]
rows.append(cells)
result.append({"rows": rows, "column_widths": table.ColumnWidths})
print(json.dumps(result, indent=2))
Usa la prima riga come intestazioni
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
rows = table.GetChildNodes(TableRow)
if not rows:
continue
def row_text(row):
return [
" ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
for cell in row.GetChildNodes(TableCell)
]
headers = row_text(rows[0])
print("Headers:", headers)
for row in rows[1:]:
record = dict(zip(headers, row_text(row)))
print(" Record:", record)
Cosa supporta la libreria per le tabelle
| Funzionalità | Supportato |
|---|---|
Table.ColumnWidths | Sì: larghezze delle colonne in punti |
Table.BordersVisible | Sì |
Table.Tags | Sì: tag OneNote sulle tabelle |
Testo della cella tramite RichText | Sì |
Immagini della cella tramite Image | Sì |
| Celle unite (metadati rowspan/colspan) | Non esposto nell’API pubblica |
Scrivi/modifica tabelle e salva su .one | No |