Python içinde OneNote Tablolarını Ayrıştır
Microsoft OneNote, kullanıcıların sayfalara doğrudan yapılandırılmış tablolar eklemesine olanak tanır; görev listeleri, takvimler, karşılaştırma matrisleri ve veri toplama formları için mükemmeldir. Aspose.Note FOSS for Python, bu tablo verilerinin tamamını programlı olarak çıkarmayı mümkün kılar; Microsoft Office kurulumu gerektirmez.
Kurulum
pip install aspose-note
Belgeyi Yükle ve Tabloları Bul
GetChildNodes(Table) tüm belge üzerinde özyinelemeli bir arama gerçekleştirir ve her tabloyu bir Table nesne:
from aspose.note import Document, Table
doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")
Hücre Değerlerini Oku
Tablolar üç seviyeli bir hiyerarşiyi izler: Table → TableRow → TableCell. Her hücre şunları içerir RichText düğümlerinin .Text düz metin içeriğini verir:
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}")
Sütun Genişliklerini İncele
Table.ColumnWidths her sütunun saklanan genişliğini puan cinsinden döndürür:
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}")
Tüm Tabloları CSV’ye Dışa Aktar
Belgedeki her tabloyu CSV formatına dönüştür:
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")
Tabloları bir Python Dict / JSON’a Dışa Aktar
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))
İlk Satırı Başlık Olarak Kullan
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)
Kitaplığın Tablolar İçin Desteklediği Özellikler
| Özellik | Desteklenir |
|---|---|
Table.ColumnWidths | Evet: sütun genişlikleri puan cinsinden |
Table.BordersVisible | Evet |
Table.Tags | Evet: Tablo üzerindeki OneNote etiketleri |
Hücre metni aracılığıyla RichText | Evet |
Hücre görselleri aracılığıyla Image | Evet |
| Birleştirilmiş hücreler (rowspan/colspan meta verileri) | Genel API’de sunulmaz |
Tabloları yazın/düzenleyin ve kaydedin .one | Hayır |