이미지 및 첨부 파일 — Aspose.Note FOSS for Python

Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one 섹션 파일. 콘텐츠는 다음을 통해 노출됩니다: ImageAttachedFile 노드 유형.


이미지 추출

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)

이미지 대체 텍스트 및 하이퍼링크 검사

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 태그

이미지와 첨부 파일의 태그 검사

둘 다 ImageAttachedFile 노드는 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 vs AttachedFile

기능ImageAttachedFile
FileName예 (원본 이미지 이름)예 (원본 파일 이름)
Bytes원시 이미지 바이트원시 파일 바이트
Width / Height예 (렌더링된 치수)아니오
AlternativeTextTitle/Description아니오
HyperlinkUrl아니오
Tags
Replace(node) 방법아니오

  • 항상 대비하십시오 None 파일 이름. img.FileName 이다 None 파일에 원본 문서에서 이름이 없을 때.
  • img.Bytes 절대 아니다 None 그리고 항상 원시 바이너리 콘텐츠이며; 자리표시자 이미지의 경우 0바이트일 수 있습니다.
  • 사용 Page.GetChildNodes(Image) 대신 Document.GetChildNodes(Image) 추출 범위를 단일 페이지로 제한하려면.
  • Image.Replace(image) 메서드는 메모리 내에서 하나의 이미지 노드의 내용을 다른 이미지 노드로 교체합니다; 다시 저장하는 것은 .one 지원되지 않습니다.

또 보기

 한국어