Hình ảnh và Tệp đính kèm — Aspose.Note FOSS cho Python
Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one các tệp section. Nội dung được hiển thị thông qua the Image và AttachedFile các loại nút.
Trích xuất Hình ảnh
Mỗi hình ảnh trong tài liệu OneNote được biểu diễn bằng một Image nút. Sử dụng GetChildNodes(Image) để truy xuất tất cả hình ảnh từ tài liệu một cách đệ quy:
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)")Thuộc tính Hình ảnh
| Thuộc tính | Kiểu | Mô tả |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | Dữ liệu hình ảnh thô (định dạng khớp với bản gốc, ví dụ PNG, JPEG) |
Width | `float | None` |
Height | `float | None` |
AlternativeTextTitle | `str | None` |
AlternativeTextDescription | `str | None` |
HyperlinkUrl | `str | None` |
Tags | list[NoteTag] | Các thẻ OneNote đính kèm vào hình ảnh này |
Lưu Hình ảnh với Tên Tệp An toàn
Khi FileName là None hoặc chứa các ký tự không an toàn cho hệ thống tệp, hãy làm sạch trước khi lưu:
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)Trích xuất Hình ảnh Theo Trang
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)Kiểm tra Văn bản Alt và Liên kết của Hình ảnh
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)Phát hiện Định dạng Tệp Hình ảnh từ Byte
Python’s imghdr module (Python ≤ 3.12) hoặc struct module có thể phát hiện định dạng hình ảnh từ các byte đầu tiên:
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)Trích xuất Tệp Đính kèm
Các tệp đính kèm nhúng được lưu dưới dạng AttachedFile các nút:
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)")Thuộc tính AttachedFile
| Thuộc tính | Kiểu | Mô tả |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | Nội dung tệp thô |
Tags | list[NoteTag] | Thẻ OneNote được gắn vào nút này |
Kiểm tra Thẻ trên Hình ảnh và Tệp Đính kèm
Cả hai Image và AttachedFile các nút hỗ trợ thẻ 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}")Tóm tắt: Image vs AttachedFile
| Tính năng | Image | AttachedFile |
|---|---|---|
FileName | Có (tên hình ảnh gốc) | Có (tên tệp gốc) |
Bytes | Byte ảnh thô | Byte tệp thô |
Width / Height | Có (kích thước đã render) | Không |
AlternativeTextTitle/Description | Có | Không |
HyperlinkUrl | Có | Không |
Tags | Có | Có |
Replace(node) phương pháp | Có | Không |
Mẹo
- Luôn luôn đề phòng
Nonetên tệp.img.FileNamelàNonekhi tệp không có tên trong tài liệu nguồn. img.Byteskhông bao giờ làNonevà luôn luôn là nội dung nhị phân thô; nó có thể là 0 byte cho các hình ảnh placeholder.- Sử dụng
Page.GetChildNodes(Image)thay vìDocument.GetChildNodes(Image)để giới hạn việc trích xuất trong một trang duy nhất. - Cái
Image.Replace(image)phương pháp thay thế nội dung của một nút hình ảnh bằng một nút khác trong bộ nhớ; việc lưu lại vào.onekhông được hỗ trợ.