テーブル解析 — Aspose.Note FOSS for Python
OneNote ドキュメントのテーブルは、3 レベルの階層として公開されます:: 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ジェネリックイテレータではなく、型付きリストが必要な場合。. IsBordersVisible保存時の OneNote ユーザーの表示設定を反映します; コンテンツ抽出には影響しません。.