Analizează tabelele OneNote în Python

Microsoft OneNote permite utilizatorilor să încorporeze tabele structurate direct în pagini, perfecte pentru liste de sarcini, programe, matrici de comparație și formulare de colectare a datelor. Aspose.Note FOSS pentru Python face posibilă extragerea programatică a tuturor acestor date tabelare, fără a necesita instalarea Microsoft Office.

Instalare

pip install aspose-note

Încarcă documentul și găsește tabelele

GetChildNodes(Table) efectuează o căutare recursivă în întregul document și returnează fiecare tabel ca un Table obiect:

from aspose.note import Document, Table

doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")

Citește valorile celulelor

Tabelele urmează o ierarhie pe trei niveluri: Table → TableRow → TableCell. Fiecare celulă conține RichText noduri ale căror .Text furnizează conținutul text simplu:

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}")

Inspectează lățimile coloanelor

Table.ColumnWidths returnează lățimea stocată a fiecărei coloane în puncte:

from aspose.note import Document, Table

doc = Document("MyNotes.one")
for i, table in enumerate(doc.GetChildNodes(Table), start=1):
    widths = [col.Width for col in table.Columns]
    print(f"Table {i}: {len(widths)} column(s)")
    print(f"  Widths (pts): {widths}")
    print(f"  Borders visible: {table.IsBordersVisible}")

Exportă toate tabelele în CSV

Convertește fiecare tabel din document în format 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")

Exportă tabelele într-un dicționar 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": [col.Width for col in table.Columns]})

print(json.dumps(result, indent=2))

Folosește primul rând ca anteturi

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)

Ce suportă biblioteca pentru tabele

FuncționalitateSuportat
Table.ColumnWidthsDa: lățimile coloanelor în puncte
Table.BordersVisibleDa
Table.TagsDa: etichete OneNote pe tabele
Textul celulei prin RichTextDa
Imaginile celulei prin ImageDa
Celule fuzionate (metadate rowspan/colspan)Nu este expus în API-ul public
Scrie/editare tabele și salvează în .oneNu

Pași următori