สกัดข้อความจากไฟล์ OneNote ด้วย Python
หากคุณต้องการอ่านข้อความจาก Microsoft OneNote .one ไฟล์ในสคริปต์ Python โดยไม่ต้องติดตั้ง Microsoft Office หรือรัน Windows, Aspose.Note FOSS for Python คือคำตอบ มันเป็นไลบรารีฟรี 100% แบบโอเพนซอร์สที่ทำการแยกรูปแบบไบนารีของ OneNote โดยตรงและเปิดเผย API Python ที่สะอาด.
ติดตั้ง
pip install aspose-note
ไม่มีคีย์ API. ไม่มีไฟล์ใบอนุญาต. ไม่มี Microsoft Office.
วิธีที่ง่ายที่สุด: GetChildNodes(RichText)
ข้อความ OneNote ถูกจัดเก็บใน RichText โหนดที่กระจายอยู่ทั่วหน้าต่าง ๆ, โครงร่าง, และองค์ประกอบของโครงร่าง. GetChildNodes(RichText) ทำการค้นหาแบบเรียกซ้ำทั่วทั้งต้นไม้ของเอกสารและคืนค่าโหนดข้อความทุกอันเป็นรายการแบน:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)
นี่คือวิธีที่เร็วที่สุดในการดึงเนื้อหาข้อความทั้งหมดออกจาก .one ไฟล์.
บันทึกข้อความลงไฟล์
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
lines = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
with open("extracted.txt", "w", encoding="utf-8") as f:
f.write("\n".join(lines))
print(f"Saved {len(lines)} text blocks to extracted.txt")
สกัดข้อความต่อหน้า
เมื่อคุณต้องการทราบว่าบล็อกข้อความแต่ละบล็อกมาจากหน้าใด:
from aspose.note import Document, Page, RichText
doc = Document("MyNotes.one")
for page in doc.GetChildNodes(Page):
title = (
page.Title.TitleText.Text
if page.Title and page.Title.TitleText
else "(untitled)"
)
page_texts = [rt.Text for rt in page.GetChildNodes(RichText) if rt.Text]
print(f"\n=== {title} ===")
for text in page_texts:
print(text)
สกัดไฮเปอร์ลิงก์
ไฮเปอร์ลิงก์ถูกจัดเก็บบนแต่ละ TextRun อ็อบเจกต์ภายใน RichText ตรวจสอบ run.Style.IsHyperlink:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.Runs:
if run.Style.IsHyperlink and run.Style.HyperlinkAddress:
print(f"{run.Text!r} -> {run.Style.HyperlinkAddress}")
ตรวจจับการจัดรูปแบบ: ตัวหนา, ตัวเอียง, ขีดเส้นใต้
แต่ละ TextRun บรรจุการจัดรูปแบบต่ออักขระผ่าน TextStyle:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.Runs:
s = run.Style
if any([s.Bold, s.Italic, s.Underline]):
flags = ", ".join(f for f, v in [
("bold", s.Bold), ("italic", s.Italic), ("underline", s.Underline)
] if v)
print(f"[{flags}] {run.Text.strip()!r}")
อ่านจากสตรีม
ทำงานกับที่เก็บข้อมูลบนคลาวด์, เนื้อหาการตอบสนอง HTTP, หรือบัฟเฟอร์ในหน่วยความจำ:
import io, urllib.request
from aspose.note import Document, RichText
##Example: load from bytes already in memory
one_bytes = open("MyNotes.one", "rb").read()
doc = Document(io.BytesIO(one_bytes))
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
print(f"Extracted {len(texts)} text block(s)")
การแก้ไขการเข้ารหัสบน Windows
บนเทอร์มินัลของ Windows, sys.stdout อาจใช้การเข้ารหัสแบบเก่าที่ทำให้แครชเมื่อเจออักขระ Unicode เพิ่มบรรทัดนี้ที่จุดเริ่มต้นของสคริปต์ของคุณ:
import sys
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
สิ่งที่ไลบรารีสนับสนุน
| ฟีเจอร์ | รองรับ |
|---|---|
อ่าน .one ไฟล์ (พาธหรือสตรีม) | ใช่ |
ดึงข้อมูล RichText.Text (ข้อความธรรมดา) | ใช่ |
ตรวจสอบ TextRun.Style (ตัวหนา, ตัวเอียง, ลิงก์, ฟอนต์) | ใช่ |
| ดึงข้อความจากเซลล์ตาราง | ใช่ |
| อ่านชื่อหน้า | ใช่ |
เขียนกลับไปยัง .one | ไม่ |
| เอกสารที่เข้ารหัส | ไม่ |