تحلیل جدول — Aspose.Note FOSS برای Python
جداول در اسناد OneNote به صورت یک سلسلهمراتب سهسطحی نمایش داده میشوند: Table → TableRow → TableCell. هر سلول میتواند شامل RichText, Image, و سایر گرههای محتوا. این صفحه تمام الگوهای تجزیه جدول پشتیبانیشده توسط Aspose.Note FOSS برای Python را پوشش میدهد.
تکرار پایه جدول
تمام جداول موجود در سند را بازیابی کنید و متن سلولهای آنها را بخوانید:
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for table_num, table in enumerate(doc.GetChildNodes(Table), start=1):
print(f"\nTable {table_num}: {len(table.Columns)} column(s)")
for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
cells = row.GetChildNodes(TableCell)
values = [
" ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
for cell in cells
]
print(f" Row {r}: {values}")ویژگیهای جدول
| ویژگی | نوع | توضیح |
|---|---|---|
Columns | list[TableColumn] | تعاریف ستون؛ هر TableColumn دارای .Width (float) و .LockedWidth (bool) |
IsBordersVisible | bool | آیا حاشیههای جدول نمایش داده میشوند |
Tags | list[NoteTag] | برچسبهای OneNote متصل به جدول |
from aspose.note import Document, Table
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
print(f"Columns: {len(table.Columns)}")
print(f"Widths (pts): {[col.Width for col in table.Columns]}")
print(f"Borders visible: {table.IsBordersVisible}")صادرات جدول به CSV
یک جدول را به فرمت CSV تبدیل کنید:
import csv
import 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 line between tables
print(output.getvalue())استخراج جداول به ازای هر صفحه
محدود کردن استخراج جدول به صفحات جداگانه:
from aspose.note import Document, Page, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for page_num, page in enumerate(doc.GetChildNodes(Page), start=1):
tables = page.GetChildNodes(Table)
if not tables:
continue
title = (
page.Title.TitleText.Text
if page.Title and page.Title.TitleText
else f"Page {page_num}"
)
print(f"\n=== {title} ({len(tables)} table(s)) ===")
for t, table in enumerate(tables, start=1):
print(f" Table {t}:")
for row in table.GetChildNodes(TableRow):
cells = row.GetChildNodes(TableCell)
row_text = [
" ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
for cell in cells
]
print(f" {row_text}")محتوای سلول فراتر از متن ساده
سلولهای جدول میتوانند شامل Image و سایر CompositeNode محتوا در کنار RichText:
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 = [rt.Text for rt in cell.GetChildNodes(RichText) if rt.Text]
images = cell.GetChildNodes(Image)
print(f" Cell texts: {texts} images: {len(images)}")شمارش و خلاصهسازی جداول
جمعآوری آمار درباره تمام جداول موجود در یک سند:
from aspose.note import Document, Table, TableRow, TableCell
doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Total tables: {len(tables)}")
for i, table in enumerate(tables, start=1):
rows = table.GetChildNodes(TableRow)
if rows:
cols = len(rows[0].GetChildNodes(TableCell))
else:
cols = 0
print(f" Table {i}: {len(rows)} row(s) x {cols} column(s) widths={[col.Width for col in table.Columns]}")بازرسی برچسبها روی جداول
جداول پشتیبانی میکنند NoteTag آیتمها بهصورت مستقیم:
from aspose.note import Document, Table, TagStatus
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
for tag in table.Tags:
is_completed = tag.Status == TagStatus.Completed
print(f"Table tag: {tag.Label} icon={tag.Icon} completed={is_completed}")موقعیت DOM جداول
جداول بهعنوان فرزندان OutlineElement گرهها درون Outline کانتینرها در هر Page. سلسلهمراتب به این صورت است:
Page
└── Outline
└── OutlineElement
└── Table
└── TableRow
└── TableCell
└── RichText / Imageهمچنین میتوانید به جداول از طریق GetChildNodes(Table) در هر سطح گرهی جدی، و تمام زیردرخت را جستجو میکند.
نکات
table.Columnsیک لیست ازTableColumnاشیاء؛ از[col.Width for col in table.Columns]برای دریافت عرض ستونها به نقطه. طول برابر با تعداد ستونها است.- محتوای سلول همیشه بهصورت خالص
RichText;؛ همیشه بررسی کنیدImageگرهها را نیز اگر صحت کامل مهم است. - استفاده کنید
table.GetChildNodes(TableRow)بهجای تکرارfor row in tableاگر به یک لیست تایپشده بهجای یک iterator عمومی نیاز دارید. IsBordersVisibleترجیح نمایش کاربر OneNote را در زمان ذخیرهسازی منعکس میکند؛ این بر استخراج محتوا تأثیری ندارد.