Python を使用して OneNote ファイルのテーブルを解析する方法

Python を使用して OneNote ファイルのテーブルを解析する方法

Microsoft OneNote は、ユーザーがページに構造化データテーブルを直接埋め込むことを可能にします。 Aspose.Note FOSS for Python は、すべてのテーブルを Table → TableRow → TableCell 階層を通じて公開し、セルの内容、列メタデータ、テーブルタグすべてにプログラムからアクセスできるようにします。

利点

  1. 構造化されたアクセス: 行と列の数、個々のセルの内容、列幅
  2. スプレッドシートアプリ不要: 任意のプラットフォームでOneNoteからテーブルデータを抽出
  3. 無料かつオープンソース: MITライセンス、APIキー不要

ステップバイステップ ガイド

ステップ 1: Python 用 Aspose.Note FOSS をインストール

pip install aspose-note

ステップ 2: .one ファイルをロードする

from aspose.note import Document

doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")

ステップ 3: すべてのテーブルを検索

GetChildNodes(Table) を使用して、ドキュメント全体からすべてのテーブルを再帰的に取得します:

from aspose.note import Document, Table

doc = Document("MyNotes.one")
tables = doc.GetChildNodes(Table)
print(f"Found {len(tables)} table(s)")

ステップ 4: 行とセルの値を読み取る

TableRowTableCell ノードを反復処理します。各セルには RichText ノードが含まれ、その .Text プロパティがプレーンテキストの内容を提供します:

from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")

for t, table in enumerate(doc.GetChildNodes(Table), start=1):
    print(f"\nTable {t}: {len(table.Columns)} column(s)")
    for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
        cell_values = []
        for cell in row.GetChildNodes(TableCell):
            text = " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
            cell_values.append(text)
        print(f"  Row {r}: {cell_values}")

ステップ 5: 列幅の読み取り

from aspose.note import Document, Table

doc = Document("MyNotes.one")
for i, table in enumerate(doc.GetChildNodes(Table), start=1):
    print(f"Table {i} column widths (pts): {[col.Width for col in table.Columns]}")
    print(f"Borders visible: {table.IsBordersVisible}")

ステップ 6: CSV にエクスポート

import csv, io
from aspose.note import Document, Table, TableRow, TableCell, RichText

doc = Document("MyNotes.one")
buf = io.StringIO()
writer = csv.writer(buf)

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(buf.getvalue())
print("Saved tables.csv")

一般的な問題と対処法

テーブルが空です

原因: セルには Image ノードが含まれており、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 = cell.GetChildNodes(RichText)
            images = cell.GetChildNodes(Image)
            print(f"  Cell: {len(texts)} text(s), {len(images)} image(s)")

列数が一致しません Columns

table.Columns はファイルに保存されている列メタデータを反映します。行に結合されたセルがある場合、実際の行あたりのセル数は異なる場合があります(ファイル形式はバイナリレベルでこれを保存していますが、パブリック API では結合フラグは公開されていません)。

ImportError: ‘aspose’というモジュールが見つかりません

pip install aspose-note
pip show aspose-note  # confirm it is installed in the active environment

よくある質問

テーブルデータを編集して保存できますか? いいえ。.one フォーマットへの書き戻しはサポートされていません。メモリ内で行われた変更(例: RichText.Replace() を使用)は、ソースファイルに永続化できません。

結合されたセルは検出されますか? CompositeNode API はマージメタデータを公開していません。各 TableCell は視覚的な結合に関係なく、個別のセルとして扱われます。

テーブルに何行あるか数えることはできますか? はい: len(table.GetChildNodes(TableRow)).


関連リソース:

 日本語