รูปภาพและไฟล์ที่แนบ — Aspose.Note FOSS สำหรับ Python
Aspose.Note FOSS for Python gives direct access to both embedded images and attached files stored inside OneNote .one ไฟล์ส่วน. เนื้อหาถูกเปิดเผยผ่าน Image และ AttachedFile ประเภทโหนด.
การสกัดรูปภาพ
รูปภาพทุกภาพในเอกสาร OneNote จะถูกแทนด้วย Image โหนด. ใช้ GetChildNodes(Image) เพื่อดึงรูปภาพทั้งหมดจากเอกสารแบบเรียกซ้ำ:
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)")คุณสมบัติของรูปภาพ
| คุณสมบัติ | ประเภท | คำอธิบาย |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | ข้อมูลภาพดิบ (รูปแบบตรงกับต้นฉบับ เช่น PNG, JPEG) |
Width | `float | None` |
Height | `float | None` |
AlternativeTextTitle | `str | None` |
AlternativeTextDescription | `str | None` |
HyperlinkUrl | `str | None` |
Tags | list[NoteTag] | แท็ก OneNote ที่แนบกับภาพนี้ |
บันทึกรูปภาพด้วยชื่อไฟล์ที่ปลอดภัย
เมื่อ FileName คือ None หรือมีอักขระที่ไม่ปลอดภัยต่อระบบไฟล์ ให้ทำความสะอาดก่อนบันทึก:
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)สกัดรูปภาพต่อหน้า
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 Text) และลิงก์
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)ตรวจจับรูปแบบไฟล์รูปภาพจากไบต์
Python’s imghdr โมดูล (Python ≤ 3.12) หรือ struct โมดูลสามารถตรวจจับรูปแบบภาพจากไบต์แรกได้:
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)การสกัดไฟล์ที่แนบ
ไฟล์แนบที่ฝังอยู่จะถูกจัดเก็บเป็น AttachedFile โหนด:
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)")คุณสมบัติของ AttachedFile
| คุณสมบัติ | ประเภท | คำอธิบาย |
|---|---|---|
FileName | `str | None` |
Bytes | bytes | เนื้อหาไฟล์ดิบ |
Tags | list[NoteTag] | แท็ก OneNote ที่แนบกับโหนดนี้ |
ตรวจสอบแท็กบนรูปภาพและไฟล์แนบ
ทั้งสอง Image และ AttachedFile โหนดสนับสนุนแท็ก 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}")สรุป: Image กับ AttachedFile
| ฟีเจอร์ | Image | AttachedFile |
|---|---|---|
FileName | ใช่ (ชื่อภาพต้นฉบับ) | ใช่ (ชื่อไฟล์ต้นฉบับ) |
Bytes | ไบต์ภาพดิบ | ไบต์ไฟล์ดิบ |
Width / Height | ใช่ (มิติที่แสดงผล) | ไม่ |
AlternativeTextTitle/Description | ใช่ | ไม่ |
HyperlinkUrl | ใช่ | ไม่ |
Tags | ใช่ | ใช่ |
Replace(node) วิธี | ใช่ | ไม่ |
เคล็ดลับ
- ระวังเสมอต่อ
Noneชื่อไฟล์.img.FileNameคือNoneเมื่อไฟล์ไม่มีชื่อในเอกสารต้นฉบับ. img.Bytesไม่เคยNoneและเป็นเนื้อหาดิบแบบไบนารีเสมอ; มันอาจเป็นศูนย์ไบต์สำหรับภาพตัวแทน.- ใช้
Page.GetChildNodes(Image)แทนDocument.GetChildNodes(Image)เพื่อจำกัดการสกัดให้เฉพาะหน้าเดียว. - นี้
Image.Replace(image)เมธอดแทนที่เนื้อหาของโหนดภาพหนึ่งด้วยโหนดภาพอื่นในหน่วยความจำ; การบันทึกกลับไปยัง.oneไม่รองรับ.