Cách Đọc Siêu Dữ Liệu Hình Ảnh từ OneNote trong Python

Cách Đọc Siêu Dữ Liệu Hình Ảnh từ OneNote trong Python

Mỗi Image nút trong tài liệu OneNote mang siêu dữ liệu cùng với các byte pixel thô: tên tệp gốc, kích thước hiển thị (chiều rộng và chiều cao tính bằng điểm), văn bản thay thế cho khả năng truy cập, và tùy chọn một URL siêu liên kết nếu hình ảnh được liên kết. Aspose.Note FOSS cho Python tiết lộ tất cả các trường này thông qua the Image lớp.


Yêu cầu trước

pip install aspose-note

Thuộc Tính Hình Ảnh

Thuộc tínhKiểuMô tả
img.BytesbytesDữ liệu hình ảnh thô. Ghi vào đĩa bằng 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]Các thẻ OneNote gắn vào hình ảnh này (ngôi sao, hộp kiểm, v.v.).

Bước 1: Tải Tài Liệu và Tìm Hình Ảnh

from aspose.note import Document, Image

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

Bước 2: Đọc Siêu Dữ Liệu cho Mỗi Hình Ảnh

Bảo vệ tất cả các trường có thể null bằng is not None trước khi sử dụng:

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

Ví Dụ Hoàn Chỉnh: Lưu Hình Ảnh kèm Báo Cáo Siêu Dữ Liệu

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

Lọc Hình Ảnh theo Thuộc Tính

Hình Ảnh có siêu liên kết

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

Hình Ảnh có văn bản thay thế

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

Ghi chú

  • img.Bytes luôn luôn tồn tại (trả về b"" đối với các hình ảnh không đọc được, không bao giờ None). Kiểm tra len(img.Bytes) > 0 trước khi lưu.
  • img.AlternativeTextTitle có thể là None nếu tài liệu nguồn không đặt tiêu đề. Sử dụng img.AlternativeTextDescription như một phương án dự phòng.
  • Kích thước được tính bằng điểm (1 điểm = 1/72 inch), phù hợp với quy ước của PowerPoint và PDF.

Xem thêm

 Tiếng Việt