Gambar dan File Terlampir — Aspose.Note FOSS untuk Python

Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one file bagian. Konten diekspos melalui the Image dan AttachedFile tipe node.


Mengekstrak Gambar

Setiap gambar dalam dokumen OneNote direpresentasikan oleh sebuah Image node. Gunakan GetChildNodes(Image) untuk mengambil semua gambar dari dokumen secara rekursif:

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)")

Properti Gambar

PropertiTipeDeskripsi
FileName`strNone`
BytesbytesData gambar mentah (format sesuai dengan aslinya, mis. PNG, JPEG)
Width`floatNone`
Height`floatNone`
AlternativeTextTitle`strNone`
AlternativeTextDescription`strNone`
HyperlinkUrl`strNone`
Tagslist[NoteTag]Tag OneNote yang terlampir pada gambar ini

Simpan Gambar dengan Nama File Aman

Kapan FileName adalah None atau mengandung karakter yang tidak aman untuk sistem file, bersihkan sebelum menyimpan:

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)

Ekstrak Gambar per Halaman

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)

Periksa Teks Alt Gambar dan Tautan

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)

Deteksi Format File Gambar dari Byte

Python’s imghdr modul (Python ≤ 3.12) atau the struct modul dapat mendeteksi format gambar dari byte pertama:

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)

Mengekstrak File Terlampir

Lampiran file tersemat disimpan sebagai AttachedFile node:

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)")

Properti AttachedFile

PropertiTipeDeskripsi
FileName`strNone`
BytesbytesKonten file mentah
Tagslist[NoteTag]Tag OneNote yang terlampir pada node ini

Periksa Tag pada Gambar dan Lampiran

Keduanya Image dan AttachedFile node mendukung tag 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}")

Ringkasan: Image vs AttachedFile

FiturImageAttachedFile
FileNameYa (nama gambar asli)Ya (nama file asli)
BytesByte gambar mentahByte file mentah
Width / HeightYa (dimensi yang dirender)Tidak
AlternativeTextTitle/DescriptionYaTidak
HyperlinkUrlYaTidak
TagsYaYa
Replace(node) metodeYaTidak

Tips

  • Selalu waspada terhadap None nama berkas. img.FileName adalah None ketika berkas tidak memiliki nama dalam dokumen sumber.
  • img.Bytes tidak pernah None dan selalu berupa konten biner mentah; dapat berukuran nol byte untuk gambar placeholder.
  • Gunakan Page.GetChildNodes(Image) daripada Document.GetChildNodes(Image) untuk membatasi ekstraksi ke satu halaman.
  • The Image.Replace(image) metode menggantikan konten satu node gambar dengan yang lain di memori; menyimpan kembali ke .one tidak didukung.

Lihat Juga

 Bahasa Indonesia