画像と添付ファイル — Aspose.Note FOSS for Python

Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one セクション ファイル。コンテンツは以下を通じて公開されます Image および AttachedFile ノード タイプ。.


画像の抽出

OneNote ドキュメント内のすべての画像は、ある Image ノードです。使用してください GetChildNodes(Image) ドキュメントからすべての画像を再帰的に取得するには:

from aspose.note import Document, Image

doc = Document("MyNotes.one")
for i, img in enumerate(doc.GetChildNodes(Image), start=1):
    filename = img.FileName or f"image_{i}.bin"
    with open(filename, "wb") as f:
        f.write(img.Bytes)
    print(f"Saved: {filename}  ({img.Width} x {img.Height} pts)")

画像プロパティ

プロパティタイプ説明
FileName`strNone`
Bytesbytes生画像データ(フォーマットは元と一致します。例: PNG、JPEG)
Width`floatNone`
Height`floatNone`
AlternativeTextTitle`strNone`
AlternativeTextDescription`strNone`
HyperlinkUrl`strNone`
Tagslist[NoteTag]この画像に付けられた OneNote タグ

安全なファイル名で画像を保存する

いつ FileName です None またはファイルシステムに安全でない文字が含まれている場合、保存する前にサニタイズしてください:

import re
from aspose.note import Document, Image

def safe_name(name: str | None, fallback: str) -> str:
    if not name:
        return fallback
    return re.sub(r'[<>:"/\\|?*]', "_", name)

doc = Document("MyNotes.one")
for i, img in enumerate(doc.GetChildNodes(Image), start=1):
    name = safe_name(img.FileName, f"image_{i}.bin")
    with open(name, "wb") as f:
        f.write(img.Bytes)

ページごとの画像抽出

from aspose.note import Document, Page, Image

doc = Document("MyNotes.one")
for page_num, page in enumerate(doc.GetChildNodes(Page), start=1):
    images = page.GetChildNodes(Image)
    print(f"Page {page_num}: {len(images)} image(s)")
    for i, img in enumerate(images, start=1):
        filename = f"page{page_num}_img{i}.bin"
        with open(filename, "wb") as f:
            f.write(img.Bytes)

画像の代替テキストとハイパーリンクを確認する

from aspose.note import Document, Image

doc = Document("MyNotes.one")
for img in doc.GetChildNodes(Image):
    if img.AlternativeTextTitle:
        print("Alt title:", img.AlternativeTextTitle)
    if img.AlternativeTextDescription:
        print("Alt desc:", img.AlternativeTextDescription)
    if img.HyperlinkUrl:
        print("Linked to:", img.HyperlinkUrl)

バイトから画像ファイル形式を検出する

Pythonの imghdr モジュール (Python ≤ 3.12) または struct モジュールは最初のバイトから画像フォーマットを検出できます:

from aspose.note import Document, Image

doc = Document("MyNotes.one")
for img in doc.GetChildNodes(Image):
    b = img.Bytes
    if b[:4] == b'\x89PNG':
        fmt = "png"
    elif b[:2] == b'\xff\xd8':
        fmt = "jpeg"
    elif b[:4] == b'GIF8':
        fmt = "gif"
    elif b[:2] in (b'BM',):
        fmt = "bmp"
    else:
        fmt = "bin"
    name = (img.FileName or f"image.{fmt}").rsplit(".", 1)[0] + f".{fmt}"
    with open(name, "wb") as f:
        f.write(b)

添付ファイルの抽出

埋め込みファイル添付は次のように保存されます AttachedFile ノード:

from aspose.note import Document, AttachedFile

doc = Document("NotesWithAttachments.one")
for i, af in enumerate(doc.GetChildNodes(AttachedFile), start=1):
    filename = af.FileName or f"attachment_{i}.bin"
    with open(filename, "wb") as f:
        f.write(af.Bytes)
    print(f"Saved attachment: {filename}  ({len(af.Bytes):,} bytes)")

AttachedFile プロパティ

プロパティタイプ説明
FileName`strNone`
Bytesbytes生のファイル内容
Tagslist[NoteTag]このノードに付随する OneNote タグ

画像と添付ファイルのタグを確認する

両方 Image および AttachedFile ノードは OneNote タグをサポートします:

from aspose.note import Document, Image, AttachedFile

doc = Document("MyNotes.one")

for img in doc.GetChildNodes(Image):
    for tag in img.Tags:
        print(f"Image tag: {tag.Label}  completedTime={tag.CompletedTime}")

for af in doc.GetChildNodes(AttachedFile):
    for tag in af.Tags:
        print(f"Attachment tag: {tag.Label}  completedTime={tag.CompletedTime}")

概要: Image と AttachedFile の比較

機能ImageAttachedFile
FileNameはい(元の画像名)はい(元のファイル名)
Bytes生画像バイト生ファイルバイト
Width / Heightはい(レンダリングされた寸法)いいえ
AlternativeTextTitle/Descriptionはいいいえ
HyperlinkUrlはいいいえ
Tagsはいはい
Replace(node) メソッドはいいいえ

ヒント

  • 常に警戒する None ファイル名。. img.FileName です None ファイルがソース文書で名前を持っていなかったとき。.
  • img.Bytes 決して None そして常に生のバイナリコンテンツです;プレースホルダー画像の場合、ゼロバイトになることがあります。.
  • 使用 Page.GetChildNodes(Image) 代わりに Document.GetChildNodes(Image) 抽出を単一ページに限定するために。.
  • その Image.Replace(image) メソッドは、ある画像ノードの内容を別の画像ノードにメモリ上で置き換えます;元に保存することは .one はサポートされていません。.

関連項目

 日本語