Hoe afbeeldingsmetadata lezen uit OneNote in Python
Elke Image node in een OneNote-document draagt metadata naast de ruwe pixelbytes: de oorspronkelijke bestandsnaam, weergavedimensies (breedte en hoogte in punten), alternatieve tekst voor toegankelijkheid, en optioneel een hyperlink-URL als de afbeelding was gekoppeld. Aspose.Note FOSS voor Python maakt al deze velden beschikbaar via de Image klasse.
Voorvereisten
pip install aspose-noteAfbeeldingseigenschappen
| Eigenschap | Type | Beschrijving |
|---|---|---|
img.Bytes | bytes | Ruwe afbeeldingsgegevens. Schrijf naar schijf met open(name, "wb").write(img.Bytes). |
img.FileName | `str | None` |
img.Width | `float | None` |
img.Height | `float | None` |
img.AlternativeTextDescription | `str | None` |
img.AlternativeTextTitle | `str | None` |
img.HyperlinkUrl | `str | None` |
img.Tags | list[NoteTag] | OneNote‑tags die aan deze afbeelding zijn gekoppeld (ster, selectievakje, enz.). |
Stap 1: Laad het document en vind afbeeldingen
from aspose.note import Document, Image
doc = Document("MyNotes.one")
images = doc.GetChildNodes(Image)
print(f"Found {len(images)} image(s)")Stap 2: Lees metadata voor elke afbeelding
Bescherm de nullable velden met is not None voordat ze worden gebruikt:
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}")Volledig voorbeeld: afbeeldingen opslaan met metadata‑rapport
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")Afbeeldingen filteren op eigenschap
Afbeeldingen met hyperlinks
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}")Afbeeldingen met alt‑tekst
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}")Opmerkingen
img.Bytesis altijd aanwezig (geeft terugb""voor onleesbare afbeeldingen, nooitNone). Controleerlen(img.Bytes) > 0voordat je opslaat.img.AlternativeTextTitlekan zijnNoneals het brondocument geen titel instelt. Gebruikimg.AlternativeTextDescriptionals fallback.- Afmetingen zijn in punten (1 punt = 1/72 inch), overeenkomstig de conventies van PowerPoint en PDF.