開発者ガイド
Aspose.Note FOSS for Python is a free, open-source library for reading Microsoft OneNote .one Microsoft Office に依存せずに section files を扱います。クリーンなパブリック API を以下で提供します。 aspose.note package は Aspose.Note for .NET インターフェースをモデルにしています。このライブラリは文書自動化、コンテンツインデックス作成、データ抽出パイプライン、アーカイブワークフローに適しています。.
この開発者ガイドは、バージョン 26.3.1 で利用可能な完全なパブリック API を網羅し、主要な機能ごとに実行可能なコード例を提供します。.
ドキュメントの読み込み
ロードする .one ファイルパスまたはバイナリストリームからファイルをロードします。The Document クラスはすべての操作のエントリーポイントです。.
ファイルパスからロード
from aspose.note import Document
doc = Document("MyNotes.one")バイナリストリームからロード
クラウドストレージ、HTTP 応答、またはメモリバッファから読み取る場合に便利です::
from pathlib import Path
from aspose.note import Document
with Path("MyNotes.one").open("rb") as f:
doc = Document(f)ロードオプション
使用する LoadOptions ロード時にオプションパラメータを設定します:
from aspose.note import Document, LoadOptions
opts = LoadOptions()
opts.LoadHistory = True # Include page history in the DOM
doc = Document("MyNotes.one", opts)注意: DocumentPassword は に存在します LoadOptions API 互換性のために提供されていますが、暗号化された文書はサポートされていません。暗号化されたファイルのロードを試みると例外が発生します。 IncorrectPasswordException.
ドキュメント構造 (DOM)
OneNote のドキュメントモデルはツリー構造です::
Document
└── Page (0..n)
├── Title
│ ├── TitleText (RichText)
│ ├── TitleDate (RichText)
│ └── TitleTime (RichText)
└── Outline (0..n)
└── OutlineElement (0..n)
├── RichText
├── Image
├── Table
│ └── TableRow
│ └── TableCell
│ └── RichText / Image
└── AttachedFileすべてのノードは次を公開します ParentNode と Document ルートまでたどるプロパティ。コンポジットノードは子のイテレーションをサポートします、, FirstChild, LastChild, AppendChildLast, InsertChild, RemoveChild,、および GetChildNodes(Type).
ページの反復
ページは の直接の子です Document.。それらを直接イテレートするか、または使用します GetChildNodes:
from aspose.note import Document, Page
doc = Document("MyNotes.one")
for page in doc:
title = page.Title.TitleText.Text if page.Title and page.Title.TitleText else "(untitled)"
author = page.Author or "(unknown)"
print(f" {title} [by {author}]")ページメタデータ::
| プロパティ | タイプ | 説明 |
|---|---|---|
Title | `Title | None` |
Author | `str | None` |
CreationTime | `datetime | None` |
LastModifiedTime | `datetime | None` |
Level | `int | None` |
テキスト抽出
すべてのプレーンテキストを抽出
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
all_text = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
print("\n".join(all_text))書式ランを検査
各 RichText はリストを含む TextRun セグメントです。各実行は独自の TextStyle:
from aspose.note import Document, RichText
doc = Document("FormattedNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
style = run.Style
flags = []
if style.IsBold: flags.append("bold")
if style.IsItalic: flags.append("italic")
if style.IsHyperlink: flags.append(f"link={style.HyperlinkAddress}")
print(f"{run.Text!r:40s} [{', '.join(flags)}]")ハイパーリンクを抽出
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(run.Text, "->", run.Style.HyperlinkAddress)画像抽出
from aspose.note import Document, Image
doc = Document("MyNotes.one")
for i, img in enumerate(doc.GetChildNodes(Image), start=1):
name = img.FileName or f"image_{i}.bin"
with open(name, "wb") as f:
f.write(img.Bytes)
print(f"Saved {name} ({img.Width}x{img.Height})")画像プロパティ: FileName, Bytes, Width, Height, AlternativeTextTitle, AlternativeTextDescription, HyperlinkUrl, Tags.
テーブル解析
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
print("Column widths:", [col.Width for col in table.Columns])
for r, row in enumerate(table.GetChildNodes(TableRow), start=1):
cells = row.GetChildNodes(TableCell)
row_text = [
" ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
for cell in cells
]
print(f"Row {r}:", row_text)添付ファイル
from aspose.note import Document, AttachedFile
doc = Document("NotesWithAttachments.one")
for i, af in enumerate(doc.GetChildNodes(AttachedFile), start=1):
name = af.FileName or f"attachment_{i}.bin"
with open(name, "wb") as f:
f.write(af.Bytes)
print(f"Saved: {name}")タグと番号付きリスト
NoteTag アイテムを検査
from aspose.note import Document, RichText, Image, Table
doc = Document("TaggedNotes.one")
for rt in doc.GetChildNodes(RichText):
for tag in rt.Tags:
print(f"RichText tag: {tag.Label} icon={tag.Icon}")
for img in doc.GetChildNodes(Image):
for tag in img.Tags:
print(f"Image tag: {tag.Label}")番号付きリストを検査
from aspose.note import Document, OutlineElement
doc = Document("NumberedNotes.one")
for oe in doc.GetChildNodes(OutlineElement):
nl = oe.NumberList
if nl:
print(f"format={nl.Format!r}")DocumentVisitor パターン
使用 DocumentVisitor ドキュメント全体のツリーを走査するビジターを実装するために:
from aspose.note import Document, DocumentVisitor, Page, RichText, Image
class ContentCounter(DocumentVisitor):
def __init__(self):
self.pages = 0
self.texts = 0
self.images = 0
def VisitPageStart(self, page: Page) -> None:
self.pages += 1
def VisitRichTextStart(self, rt: RichText) -> None:
self.texts += 1
def VisitImageStart(self, img: Image) -> None:
self.images += 1
doc = Document("MyNotes.one")
counter = ContentCounter()
doc.Accept(counter)
print(f"Pages: {counter.pages}, Texts: {counter.texts}, Images: {counter.images}")PDF エクスポート
PDF エクスポートにはオプションの ReportLab 依存関係が必要です。以下でインストールしてください:
pip install "aspose-note[pdf]"基本的な PDF エクスポート
from aspose.note import Document, SaveFormat
doc = Document("MyNotes.one")
doc.Save("output.pdf", SaveFormat.Pdf)オプション付き PDF エクスポート
import io
from aspose.note import Document, SaveFormat
from aspose.note.saving import PdfSaveOptions
doc = Document("MyNotes.one")
##With save options
opts = PdfSaveOptions()
doc.Save("output.pdf", opts)
##Save to in-memory stream
buf = io.BytesIO()
doc.Save(buf, PdfSaveOptions())
pdf_bytes = buf.getvalue()注記: PdfSaveOptions.PageIndex および PageCount フィールドは存在しますが、v26.3.1 の PDF エクスポーターには転送されません。ドキュメント全体は常にエクスポートされます。.
現在の制限事項
| エリア | ステータス |
|---|---|
読み取り .one ファイル | 完全にサポートされています |
| PDF エクスポート(ReportLab 経由) | サポート済み |
書き戻し先 .one | 未実装 |
| 暗号化された文書 | サポートされていません(raises IncorrectPasswordException) |
| HTML / 画像 / ONE 保存形式 | API 互換性のために宣言されています;raise UnsupportedSaveFormatException |