تصاویر و فایل‌های پیوست — Aspose.Note FOSS برای 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)

بررسی متن Alt تصویر و پیوندهای ابرمتنی

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’s 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 پشتیبانی نمی‌شود.

همچنین ببینید

 فارسی