تجزیه جداول OneNote در Python
Microsoft OneNote به کاربران اجازه میدهد جداول ساختاریافته را مستقیماً در صفحات جاسازی کنند، که برای فهرستهای کار، برنامهها، ماتریسهای مقایسهای و فرمهای جمعآوری دادهها ایدهآل است. Aspose.Note FOSS برای Python امکان استخراج تمام این دادههای جدولی را بهصورت برنامهنویسی فراهم میکند، بدون نیاز به نصب Microsoft Office.
نصب
pip install aspose-note
بارگذاری سند و یافتن جداول
GetChildNodes(Table) یک جستجوی بازگشتی در سراسر سند انجام میدهد و هر جدول را به عنوان یک Table شیء:
from aspose.note import Document, Table
doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")
خواندن مقادیر سلولها
جداول یک سلسلهمراتب سهسطحی را دنبال میکنند: Table → TableRow → TableCell. هر سلول شامل RichText گرههایی که .Text محتویات متن ساده را میدهد:
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}")
بازرسی عرض ستونها
Table.ColumnWidths عرض ذخیرهشده هر ستون را به نقطه برمیگرداند:
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}")
صادر کردن تمام جداول به CSV
تبدیل هر جدول در سند به فرمت 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")
صادر کردن جداول به یک دیکشنری 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))
استفاده از ردیف اول بهعنوان سرستونها
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)
آنچه کتابخانه برای جداول پشتیبانی میکند
| ویژگی | پشتیبانی میشود |
|---|---|
Table.ColumnWidths | بله: عرض ستونها به نقطه |
Table.BordersVisible | بله |
Table.Tags | بله: برچسبهای OneNote روی جداول |
متن سلول از طریق RichText | بله |
تصاویر سلول از طریق Image | بله |
| سلولهای ادغامشده (متادیتای rowspan/colspan) | در API عمومی در دسترس نیست |
نوشتن/ویرایش جداول و ذخیره به .one | خیر |