Kā analizēt tabulas OneNote failos, izmantojot Python
Microsoft OneNote ļauj lietotājiem iekļaut strukturētas datu tabulas tieši lapās. Aspose.Note FOSS for Python atklāj katru tabulu caur Table → TableRow → TableCell hierarhiju, nodrošinot programmatiski piekļuvi visam šūnu saturam, kolonnu metadatiem un tabulu tagiem.
Ieguvumi
- Strukturēta piekļuve: rindu un kolonnu skaits, atsevišķas šūnas saturs, kolonnu platumi
- Nav vajadzīga izklājlapu lietotne: izvilkt tabulas datus no OneNote jebkurā platformā
- Bezmaksas un atvērtā pirmkoda: MIT licence, nav API atslēgas
Solī pa solim rokasgrāmata
1. solis: Instalējiet Aspose.Note FOSS priekš Python
pip install aspose-note2. solis: ielādēt .one failu
from aspose.note import Document
doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")3. solis: Atrast visas tabulas
Izmantojiet GetChildNodes(Table), lai rekursīvi izgūtu visas tabulas no visa dokumenta:
from aspose.note import Document, Table
doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")4. solis: Nolasīt rindas un šūnas vērtības
Iterējiet TableRow un TableCell mezglus. Katra šūna satur RichText mezglus, kuru .Text īpašība sniedz vienkāršo teksta saturu:
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for t, table in enumerate(doc.GetChildNodes(Table), start=1):
print(f"\nTable {t}: {len(table.Columns)} column(s)")
for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
cell_values = []
for cell in row.GetChildNodes(TableCell):
text = " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
cell_values.append(text)
print(f" Row {r}: {cell_values}")5. solis: Nolasīt kolonnas platumus
from aspose.note import Document, Table
doc = Document("MyNotes.one")
for i, table in enumerate(doc.GetChildNodes(Table), start=1):
print(f"Table {i} column widths (pts): {[col.Width for col in table.Columns]}")
print(f"Borders visible: {table.IsBordersVisible}")6. solis: Eksportēt uz CSV
import csv, io
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
buf = io.StringIO()
writer = csv.writer(buf)
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(buf.getvalue())
print("Saved tables.csv")Biežas problēmas un risinājumi
Tabulas izskatās tukšas
Iemesls: Šūnas satur Image mezglus, nevis RichText mezglus.
Pārbaude:
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 = cell.GetChildNodes(RichText)
images = cell.GetChildNodes(Image)
print(f" Cell: {len(texts)} text(s), {len(images)} image(s)")Kolonnu skaits neatbilst Columns
table.Columns atspoguļo kolonnas metadatus, kas saglabāti failā. Faktiskais šūnu skaits rindā var atšķirties, ja rindās ir apvienotas šūnas (faila formāts saglabā šo binārā līmenī; publiskā API neizpauž apvienošanas karogu).
ImportError: Nav moduļa ar nosaukumu ‘aspose’
pip install aspose-note
pip show aspose-note # confirm it is installed in the active environmentBiežāk uzdotie jautājumi
Vai es varu rediģēt tabulas datus un saglabāt tos atpakaļ? Nē. Atgrieztā rakstīšana uz .one formātu netiek atbalstīta. Izmaiņas, kas veiktas atmiņā (piemēram, izmantojot RichText.Replace()), nevar saglabāt avota failā.
Vai apvienotās šūnas tiek noteiktas? CompositeNode API neatklāj sapludināšanas metadatus. Katrs TableCell tiek uzskatīts par atsevišķu šūnu neatkarīgi no vizuālās sapludināšanas.
Vai es varu saskaitīt, cik rindas ir tabulā? Jā: len(table.GetChildNodes(TableRow)).
Saistītie resursi: