OneNote から画像メタデータを Python で読む方法

OneNote から画像メタデータを Python で読む方法

すべての Image OneNote ドキュメント内のノードは、生のピクセルバイトと共にメタデータを保持します:元のファイル名、表示サイズ(ポイント単位の幅と高さ)、アクセシビリティ用の代替テキスト、そして画像がリンクされている場合はオプションでハイパーリンク URL もです。Aspose.Note FOSS for Python は、これらすべてのフィールドを通じて Image クラス。.


前提条件

pip install aspose-note

画像プロパティ

プロパティ説明
img.Bytesbytes生画像データ。ディスクに書き込むには open(name, "wb").write(img.Bytes).
img.FileName`strNone`
img.Width`floatNone`
img.Height`floatNone`
img.AlternativeTextDescription`strNone`
img.AlternativeTextTitle`strNone`
img.HyperlinkUrl`strNone`
img.Tagslist[NoteTag]この画像に付けられた OneNote タグ(星、チェックボックスなど)。.

ステップ 1: ドキュメントを読み込み、画像を検索する

from aspose.note import Document, Image

doc = Document("MyNotes.one")
images = doc.GetChildNodes(Image)
print(f"Found {len(images)} image(s)")

ステップ 2: 各画像のメタデータを読み取る

すべての nullable フィールドを以下でガードする is not None 使用前に::

from aspose.note import Document, Image

doc = Document("MyNotes.one")

for i, img in enumerate(doc.GetChildNodes(Image), start=1):
    print(f"\nImage {i}:")
    print(f"  Filename:    {img.FileName or '(no filename)'}")
    print(f"  Size:        {img.Bytes and len(img.Bytes):,} bytes")

    if img.Width is not None and img.Height is not None:
        print(f"  Dimensions:  {img.Width:.1f} × {img.Height:.1f} pts")

    if img.AlternativeTextDescription:
        print(f"  Alt text:    {img.AlternativeTextDescription}")

    if img.HyperlinkUrl:
        print(f"  Hyperlink:   {img.HyperlinkUrl}")

    if img.Tags:
        for tag in img.Tags:
            print(f"  Tag:         {tag.Label or tag.Icon}")

完全な例: メタデータレポートとともに画像を保存する

from pathlib import Path
from aspose.note import Document, Image

def report_and_save_images(one_path: str, out_dir: str = "images") -> None:
    doc = Document(one_path)
    images = doc.GetChildNodes(Image)
    if not images:
        print("No images found.")
        return

    out = Path(out_dir)
    out.mkdir(exist_ok=True)

    for i, img in enumerate(images, start=1):
        # Determine save name
        name = img.FileName or f"image_{i}.bin"
        dest = out / name

        # Save bytes
        dest.write_bytes(img.Bytes)

        # Report metadata
        dims = (
            f"{img.Width:.0f}×{img.Height:.0f}pts"
            if img.Width is not None and img.Height is not None
            else "unknown size"
        )
        alt = img.AlternativeTextDescription or ""
        link = img.HyperlinkUrl or ""

        print(f"  [{i}] {name}  {dims}"
              + (f"  alt='{alt}'" if alt else "")
              + (f"  url={link}" if link else ""))

    print(f"\nSaved {len(images)} image(s) to '{out_dir}/'")

report_and_save_images("MyNotes.one")

プロパティで画像をフィルタリングする

ハイパーリンク付き画像

from aspose.note import Document, Image

doc = Document("MyNotes.one")
linked = [img for img in doc.GetChildNodes(Image) if img.HyperlinkUrl]
for img in linked:
    print(f"{img.FileName or 'image'}{img.HyperlinkUrl}")

代替テキスト付き画像

from aspose.note import Document, Image

doc = Document("MyNotes.one")
with_alt = [img for img in doc.GetChildNodes(Image) if img.AlternativeTextDescription]
for img in with_alt:
    print(f"{img.FileName}: {img.AlternativeTextDescription}")

注釈

  • img.Bytes 常に存在する(返す b"" 読めない画像に対しては、決して None)。チェック len(img.Bytes) > 0 保存する前に。.
  • img.AlternativeTextTitle になる可能性があります None ソース文書がタイトルを設定していない場合。使用 img.AlternativeTextDescription 代替として。.
  • 寸法は ポイント (1ポイント = 1/72インチ)、PowerPoint と PDF の規約に合わせています。.

参照

 日本語