Python에서 OneNote 이미지 메타데이터 읽는 방법

Python에서 OneNote 이미지 메타데이터 읽는 방법

OneNote 문서의 모든 Image 노드는 원시 픽셀 바이트와 함께 메타데이터를 포함합니다: 원본 파일 이름, 표시 차원(포인트 단위의 너비와 높이), 접근성을 위한 대체 텍스트, 그리고 이미지가 링크된 경우 선택적으로 하이퍼링크 URL이 포함됩니다. Aspose.Note FOSS for Python은 이러한 모든 필드를 Image 클래스를 통해 노출합니다.


전제 조건

pip install aspose-note

이미지 속성

PropertyTypeDescription
img.Bytesbytes원시 이미지 데이터. open(name, "wb").write(img.Bytes)을(를) 사용하여 디스크에 기록합니다.
img.FileNamestr | None원본 파일 이름이 .one 파일에 저장됩니다. 저장되지 않은 경우 None.
img.Widthfloat | None포인트 단위의 표시 너비. 저장되지 않은 경우 None.
img.Heightfloat | None포인트 단위의 표시 높이. 저장되지 않은 경우 None.
img.AlternativeTextDescriptionstr | None접근성 대체 텍스트 본문. 설정되지 않은 경우 None.
img.AlternativeTextTitlestr | None접근성 대체 텍스트 제목. 설정되지 않은 경우 None.
img.HyperlinkUrlstr | None이미지가 클릭 가능한 하이퍼링크인 경우 URL. 연결되지 않은 경우 None.
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)")

Step 2: 각 이미지에 대한 메타데이터 읽기

사용하기 전에 is not None 로 모든 nullable 필드를 보호하십시오:

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.AlternativeTextTitleNone일 수 있습니다. 대체값으로 img.AlternativeTextDescription을 사용하십시오.
  • 차원은 포인트 단위이며 (1 포인트 = 1/72 인치), PowerPoint 및 PDF 규격과 일치합니다.

참고

 한국어