Immagini e File Allegati — Aspose.Note FOSS per Python
Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one file di sezione. Il contenuto è esposto tramite il Image e AttachedFile tipi di nodo.
Estrazione delle immagini
Ogni immagine in un documento OneNote è rappresentata da un Image nodo. Usa GetChildNodes(Image) per recuperare tutte le immagini dal documento in modo ricorsivo:
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)")Proprietà dell’immagine
| Proprietà | Tipo | Descrizione |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | Dati grezzi dell’immagine (il formato corrisponde all’originale, ad es. PNG, JPEG) |
Width | `float | None` |
Height | `float | None` |
AlternativeTextTitle | `str | None` |
AlternativeTextDescription | `str | None` |
HyperlinkUrl | `str | None` |
Tags | list[NoteTag] | Tag OneNote allegati a questa immagine |
Salva le immagini con nomi file sicuri
Quando FileName è None o contiene caratteri non sicuri per il file system, sanitizza prima di salvare:
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)Estrai le immagini per pagina
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)Ispeziona il testo alternativo e i collegamenti ipertestuali delle immagini
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)Rileva il formato del file immagine dai byte
Python’s imghdr modulo (Python ≤ 3.12) o il struct Il modulo può rilevare il formato dell’immagine dai primi byte:
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)Estrazione dei file allegati
Gli allegati di file incorporati sono memorizzati come AttachedFile nodi:
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)")Proprietà di AttachedFile
| Proprietà | Tipo | Descrizione |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | Contenuto grezzo del file |
Tags | list[NoteTag] | Tag OneNote allegati a questo nodo |
Ispeziona i tag su immagini e allegati
Entrambi Image e AttachedFile i nodi supportano i tag 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}")Riepilogo: Image vs AttachedFile
| Funzionalità | Image | AttachedFile |
|---|---|---|
FileName | Sì (nome immagine originale) | Sì (nome file originale) |
Bytes | Byte immagine grezzi | Byte file grezzi |
Width / Height | Sì (dimensioni renderizzate) | No |
AlternativeTextTitle/Description | Sì | No |
HyperlinkUrl | Sì | No |
Tags | Sì | Sì |
Replace(node) metodo | Sì | No |
Suggerimenti
- Proteggi sempre da
Nonenomi file.img.FileNameèNonequando il file non aveva un nome nel documento di origine. img.Bytesnon è maiNoneed è sempre il contenuto binario grezzo; può essere di zero byte per le immagini segnaposto.- Usa
Page.GetChildNodes(Image)invece diDocument.GetChildNodes(Image)per limitare l’estrazione a una singola pagina. - Il
Image.Replace(image)il metodo sostituisce il contenuto di un nodo immagine con un altro in memoria; il salvataggio di nuovo su.onenon è supportato.