Python を使用して OneNote ファイルからテキストを抽出

Microsoft OneNote からテキストを読み取る必要がある場合 .one Microsoft Office をインストールせず、Windows を実行せずに、Python スクリプト内のファイルを、, Aspose.Note FOSS for Python が解決策です。これは OneNote のバイナリ形式を直接解析し、クリーンな Python API を提供する、100% 無料のオープンソースライブラリです。.

インストール

pip install aspose-note

API キーは不要です。ライセンス ファイルも不要です。Microsoft Office も不要です。.


最もシンプルなアプローチ: GetChildNodes(RichText)

OneNote のテキストは次の場所に保存されます RichText ページ、アウトライン、およびアウトライン要素に分散されたノードです。. GetChildNodes(RichText) ドキュメント全体のツリーを再帰的に検索し、すべてのテキストノードをフラットなリストとして返します:

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    if rt.Text:
        print(rt.Text)

これは、すべてのテキストコンテンツを取得する最速の方法です .one ファイルから。.


テキストをファイルに保存

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
lines = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]

with open("extracted.txt", "w", encoding="utf-8") as f:
    f.write("\n".join(lines))

print(f"Saved {len(lines)} text blocks to extracted.txt")

ページごとにテキストを抽出

各テキストブロックがどのページから来たかを知りたいときは:

from aspose.note import Document, Page, RichText

doc = Document("MyNotes.one")
for page in doc.GetChildNodes(Page):
    title = (
        page.Title.TitleText.Text
        if page.Title and page.Title.TitleText
        else "(untitled)"
    )
    page_texts = [rt.Text for rt in page.GetChildNodes(RichText) if rt.Text]
    print(f"\n=== {title} ===")
    for text in page_texts:
        print(text)

ハイパーリンクを抽出

ハイパーリンクは個々の TextRun オブジェクト内の RichText ノードに保存されています。確認してください run.Style.IsHyperlink:

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.Runs:
        if run.Style.IsHyperlink and run.Style.HyperlinkAddress:
            print(f"{run.Text!r}  ->  {run.Style.HyperlinkAddress}")

書式検出: 太字、斜体、下線

TextRun 文字ごとの書式設定を通じて保持します TextStyle:

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.Runs:
        s = run.Style
        if any([s.Bold, s.Italic, s.Underline]):
            flags = ", ".join(f for f, v in [
                ("bold", s.Bold), ("italic", s.Italic), ("underline", s.Underline)
            ] if v)
            print(f"[{flags}] {run.Text.strip()!r}")

ストリームから読み取る

クラウドストレージ、HTTP レスポンスボディ、またはメモリバッファでも動作します:

import io, urllib.request
from aspose.note import Document, RichText

##Example: load from bytes already in memory
one_bytes = open("MyNotes.one", "rb").read()
doc = Document(io.BytesIO(one_bytes))
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
print(f"Extracted {len(texts)} text block(s)")

Windows エンコーディングの修正

Windows の端末では、, sys.stdout Unicode 文字でクラッシュするレガシーエンコーディングを使用する可能性があります。スクリプトの先頭にこれを追加してください::

import sys
if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")

ライブラリがサポートするもの

機能サポート済み
読み取り .one ファイル(パスまたはストリーム)はい
抽出 RichText.Text (プレーンテキスト)はい
検査 TextRun.Style (太字、斜体、ハイパーリンク、フォント)はい
テーブルセルからテキストを抽出はい
ページタイトルを読むはい
書き戻す .oneいいえ
暗号化された文書いいえ

次のステップ