تحليل جداول 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 الوصفية) | غير متاح في واجهة برمجة التطبيقات العامة |
كتابة/تحرير الجداول وحفظ إلى .one | لا |