Python에서 OneNote에 첨부된 파일 저장 방법

Python에서 OneNote에 첨부된 파일 저장 방법

OneNote .one 파일은 임베드된 파일 첨부를 포함할 수 있습니다: OneNote에서 Insert → File Attachment를 사용하여 페이지에 삽입된 모든 파일 유형. Aspose.Note FOSS for Python은 이러한 파일들을 AttachedFile 클래스를 통해 노출하며, 이 클래스는 원본 파일 이름과 임베드된 파일의 원시 바이트를 제공합니다.


전제 조건

pip install aspose-note

Step 1: 문서 로드

from aspose.note import Document

doc = Document("MyNotes.one")

2단계: 모든 첨부 파일 찾기

GetChildNodes(AttachedFile)을 사용하면 문서에 포함된 모든 첨부 파일을 페이지나 개요와 관계없이 재귀적으로 수집할 수 있습니다:

from aspose.note import Document, AttachedFile

doc = Document("MyNotes.one")
attachments = doc.GetChildNodes(AttachedFile)
print(f"Found {len(attachments)} attachment(s)")

단계 3: 각 첨부 파일을 디스크에 저장

원시 파일 내용은 af.Bytes을, 원본 이름은 af.FileName을 사용하십시오. 항상 None 파일 이름을 주의하십시오: 파일에 파일 이름 메타데이터가 저장되지 않은 경우 라이브러리는 None을 반환합니다:

from aspose.note import Document, AttachedFile

doc = Document("MyNotes.one")

for i, af in enumerate(doc.GetChildNodes(AttachedFile), start=1):
    name = af.FileName or f"attachment_{i}.bin"
    with open(name, "wb") as f:
        f.write(af.Bytes)
    print(f"Saved: {name} ({len(af.Bytes):,} bytes)")

전체 예제

이 스크립트는 .one 파일에서 모든 첨부 파일을 추출하여 전용 출력 디렉터리에 저장합니다:

from pathlib import Path
from aspose.note import Document, AttachedFile

def save_all_attachments(one_path: str, out_dir: str = "attachments") -> None:
    doc = Document(one_path)
    out = Path(out_dir)
    out.mkdir(exist_ok=True)

    attachments = doc.GetChildNodes(AttachedFile)
    if not attachments:
        print("No attachments found.")
        return

    for i, af in enumerate(attachments, start=1):
        name = af.FileName or f"attachment_{i}.bin"
        dest = out / name
        dest.write_bytes(af.Bytes)
        print(f"  [{i}] {name}  ({len(af.Bytes):,} bytes)")

    print(f"\nSaved {len(attachments)} file(s) to '{out_dir}/'")

save_all_attachments("MyNotes.one")

노트

  • af.Bytes은(는) 첨부 파일 데이터를 바이너리 파일에서 구문 분석할 수 없을 때 b""(빈 바이트)를 반환합니다. 빈 첨부 파일을 건너뛰려면 저장하기 전에 len(af.Bytes) > 0을(를) 확인하십시오.
  • 첨부 파일에 OneNote 태그가 적용된 경우 af.TagsNoteTag 객체의 목록입니다.
  • Aspose.Note FOSS for Python은 .one 파일을 읽지만 .one에 다시 쓰지는 않습니다. 첨부 파일을 생성하거나 수정할 수 없습니다.

관련 항목

 한국어