Com llegir les metadades d'imatge de OneNote en Python
Cada node Image en un document OneNote porta metadades juntament amb els bytes de píxel en brut: el nom de fitxer original, les dimensions de visualització (amplada i alçada en punts), text alternatiu per a l’accessibilitat i, opcionalment, una URL d’enllaç si la imatge estava enllaçada. Aspose.Note FOSS per a Python exposa tots aquests camps a través de la classe Image.
Requisits previs
pip install aspose-notePropietats de la imatge
| Propietat | Tipus | Descripció |
|---|---|---|
img.Bytes | bytes | Dades d’imatge en brut. Escriu al disc amb open(name, "wb").write(img.Bytes). |
img.FileName | str | None | Nom de fitxer original emmagatzemat al fitxer .one. None si no s’emmagatzema. |
img.Width | float | None | Amplada de visualització en punts. None si no s’emmagatzema. |
img.Height | float | None | Alçada de visualització en punts. None si no s’emmagatzema. |
img.AlternativeTextDescription | str | None | Cos del text alternatiu d’accessibilitat. None si no està establert. |
img.AlternativeTextTitle | str | None | Títol del text alternatiu d’accessibilitat. None si no està establert. |
img.HyperlinkUrl | str | None | URL si la imatge és un enllaç clicable. None si no està enllaçada. |
img.Tags | list[NoteTag] | Etiquetes OneNote adjuntes a aquesta imatge (estrella, casella de verificació, etc.). |
Pas 1: Carrega el document i troba les imatges
from aspose.note import Document, Image
doc = Document("MyNotes.one")
images = doc.GetChildNodes(Image)
print(f"Found {len(images)} image(s)")Pas 2: Llegir les Metadades de Cada Imatge
Protegiu tots els camps nul·les amb is not None abans d’utilitzar-los:
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}")Exemple complet: desar imatges amb informe de metadades
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")Filtra imatges per propietat
Imatges amb enllaços
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}")Imatges amb text alternatiu
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}")Notes
img.Bytesestà sempre present (retornab""per a imatges il·legibles, maiNone). Comproveulen(img.Bytes) > 0abans de desar.img.AlternativeTextTitlepot serNonesi el document d’origen no estableix un títol. Utilitzeuimg.AlternativeTextDescriptioncom a alternativa.- Les dimensions són en points (1 point = 1/72 polzada), coincidint amb les convencions de PowerPoint i PDF.