テキスト抽出 — Aspose.Note FOSS for Python
Aspose.Note FOSS for Python exposes the full text content of every OneNote page through the RichText ノード。各 RichText はプレーンテキストと .Text 文字列と .TextRuns 個別にスタイル付けされたリスト TextRun セグメント。このページでは、利用可能なすべてのテキスト抽出パターンを文書化しています。.
すべてのプレーンテキストを抽出
ドキュメントからすべてのテキストを取得する最速の方法は GetChildNodes(RichText),、これは全DOMに対して再帰的な深さ優先トラバーサルを実行します:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)リストに収集して結合します:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
all_text = "\n".join(
rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text
)ページごとにテキストを抽出
抽出したテキストをページタイトルで整理します:
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)"
)
print(f"\n=== {title} ===")
for rt in page.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)書式ランを検査
RichText.TextRuns はリストです TextRun オブジェクトです。各ランは、一様な TextStyle:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
style = run.Style
parts = []
if style.IsBold: parts.append("bold")
if style.IsItalic: parts.append("italic")
if style.IsUnderline: parts.append("underline")
if style.IsStrikethrough: parts.append("strikethrough")
if style.IsSuperscript: parts.append("superscript")
if style.IsSubscript: parts.append("subscript")
if style.FontName: parts.append(f"font={style.FontName!r}")
if style.FontSize: parts.append(f"size={style.FontSize}pt")
label = ", ".join(parts) if parts else "plain"
print(f"[{label}] {run.Text!r}")TextStyle プロパティリファレンス
| プロパティ | タイプ | 説明 |
|---|---|---|
IsBold | bool | 太字テキスト |
IsItalic | bool | 斜体テキスト |
IsUnderline | bool | 下線付きテキスト |
IsStrikethrough | bool | 取り消し線テキスト |
IsSuperscript | bool | 上付き文字 |
IsSubscript | bool | 下付き文字 |
FontName | `str | None` |
FontSize | `float | None` |
FontColor | `int | None` |
Highlight | `int | None` |
Language | `int | None` |
IsHyperlink | bool | このランがハイパーリンクかどうか |
HyperlinkAddress | `str | None` |
ハイパーリンクを抽出
ハイパーリンクは TextRun レベルです。確認してください Style.IsHyperlink:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
if run.Style.IsHyperlink and run.Style.HyperlinkAddress:
print(f" {run.Text!r:40s} -> {run.Style.HyperlinkAddress}")太字およびハイライトされたテキストを抽出
書式プロパティでランをフィルタリングし、特定のコンテンツを抽出します:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
print("=== Bold segments ===")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
if run.Style.IsBold and run.Text.strip():
print(f" {run.Text.strip()!r}")
print("\n=== Highlighted segments ===")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
if run.Style.Highlight is not None and run.Text.strip():
color = f"#{run.Style.Highlight & 0xFFFFFF:06X}"
print(f" [{color}] {run.Text.strip()!r}")タイトルブロックからテキストを抽出
ページタイトルは RichText ノードは Title オブジェクトです。トップレベルでは返されません GetChildNodes(RichText) ページ上では、次を含めない限り Title サブツリーです。直接アクセスしてください:
from aspose.note import Document, Page
doc = Document("MyNotes.one")
for page in doc.GetChildNodes(Page):
if page.Title:
if page.Title.TitleText:
print("Title text:", page.Title.TitleText.Text)
if page.Title.TitleDate:
print("Title date:", page.Title.TitleDate.Text)
if page.Title.TitleTime:
print("Title time:", page.Title.TitleTime.Text)テーブルからテキストを抽出
テーブルセルは含む RichText 子要素。ネストされた GetChildNodes 呼び出し:
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
for row in table.GetChildNodes(TableRow):
row_values = []
for cell in row.GetChildNodes(TableCell):
cell_text = " ".join(
rt.Text for rt in cell.GetChildNodes(RichText)
).strip()
row_values.append(cell_text)
print(row_values)メモリ内テキスト操作
テキストの置換
RichText.Replace(old_value, new_value) すべての実行でメモリ内のテキストを置換します:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
rt.Replace("TODO", "DONE")
##Changes are in-memory only; saving back to .one is not supportedテキストランを追加する
from aspose.note import Document, RichText, TextStyle
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
rt.Append(" [reviewed]") # appends with default style
break # just the first node in this example抽出したテキストをファイルに保存する
import sys
from aspose.note import Document, RichText
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
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"Extracted {len(lines)} text blocks.")ヒント
GetChildNodes(RichText)上でDocument検索します 全体の ツリー全体(すべてのページ、アウトライン、アウトライン要素を含む)。特定のPage範囲を限定するために。.- 常に確認してください
rt.Text(またはif rt.Text:) 印刷する前に、空としてRichTextノードは一部のドキュメントに存在します。. - Windows では、再構成してください
sys.stdoutUTF-8 に設定して、回避するUnicodeEncodeErrorシステムコードページ外の文字を印刷する際に。. TextRunはのみTextとStyleフィールドです。ありませんStart/Endオフセットプロパティはありません。ランのテキストを親内で位置特定するにはRichText.Text,、検索してくださいrun.Text内でrt.Text手動で.