Afbeeldingen en Bijgevoegde Bestanden — Aspose.Note FOSS voor Python
Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one sectiebestanden. Inhoud wordt blootgesteld via de Image en AttachedFile knooppunt types.
Afbeeldingen extraheren
Elke afbeelding in een OneNote-document wordt weergegeven door een Image knooppunt. Gebruik GetChildNodes(Image) om alle afbeeldingen uit het document recursief op te halen:
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)")Afbeeldingseigenschappen
| Eigenschap | Type | Beschrijving |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | Ruwe afbeeldingsgegevens (formaat komt overeen met het origineel, bijv. PNG, JPEG) |
Width | `float | None` |
Height | `float | None` |
AlternativeTextTitle | `str | None` |
AlternativeTextDescription | `str | None` |
HyperlinkUrl | `str | None` |
Tags | list[NoteTag] | OneNote-tags gekoppeld aan deze afbeelding |
Afbeeldingen opslaan met veilige bestandsnamen
Wanneer FileName is None of bevat tekens die onveilig zijn voor het bestandssysteem, reinig vóór het opslaan:
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)Afbeeldingen per pagina extraheren
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-tekst en hyperlinks van afbeeldingen inspecteren
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)Detecteer afbeeldingsbestandformaat vanuit bytes
Python’s imghdr module (Python ≤ 3.12) of de struct module kan het afbeeldingsformaat detecteren vanaf de eerste bytes:
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)Bijgevoegde bestanden extraheren
Ingesloten bestandsbijlagen worden opgeslagen als AttachedFile knooppunten:
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)")Eigenschappen van AttachedFile
| Eigenschap | Type | Beschrijving |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | Ruwe bestandsinhoud |
Tags | list[NoteTag] | OneNote‑tags gekoppeld aan dit knooppunt |
Tags op afbeeldingen en bijlagen inspecteren
Beide Image en AttachedFile knooppunten ondersteunen OneNote‑tags:
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}")Samenvatting: Image vs AttachedFile
| Functie | Image | AttachedFile |
|---|---|---|
FileName | Ja (originele afbeeldingsnaam) | Ja (originele bestandsnaam) |
Bytes | Ruwe afbeeldingsbytes | Ruwe bestandsbytes |
Width / Height | Ja (gerenderde afmetingen) | Nee |
AlternativeTextTitle/Description | Ja | Nee |
HyperlinkUrl | Ja | Nee |
Tags | Ja | Ja |
Replace(node) methode | Ja | Nee |
Tips
- Wees altijd op je hoede voor
Nonebestandsnamen.img.FileNameisNonewanneer het bestand geen naam had in het brondocument. img.Bytesis nooitNoneen is altijd de ruwe binaire inhoud; het kan nul bytes zijn voor tijdelijke afbeeldingen.- Gebruik
Page.GetChildNodes(Image)in plaats vanDocument.GetChildNodes(Image)om extractie te beperken tot één pagina. - De
Image.Replace(image)methode vervangt de inhoud van één afbeeldingsknooppunt door een andere in-memory; terug opslaan naar.onewordt niet ondersteund.