Cara Mengekstrak Teks dari File OneNote di Python
File Microsoft OneNote .one adalah dokumen biner yang tidak dapat dibaca sebagai teks biasa atau diparsing dengan alat XML generik. Aspose.Note FOSS for Python menyediakan parser murni‑Python yang memuat file .one ke dalam model objek dokumen (DOM) lengkap, sehingga memudahkan ekstraksi teks, metadata pemformatan, dan hyperlink secara programatik.
Manfaat Menggunakan Aspose.Note FOSS untuk Python
- Tidak memerlukan Microsoft Office: baca file
.onedi platform apa pun, termasuk server Linux CI/CD - Akses teks lengkap dan pemformatan: teks biasa, rangkaian tebal/miring/garis bawah, properti font, dan URL tautan hiperteks
- Gratis dan sumber terbuka: lisensi MIT, tanpa biaya penggunaan atau kunci API
Panduan Langkah-demi-Langkah
Langkah 1: Instal Aspose.Note FOSS untuk Python
Instal perpustakaan dari PyPI. Paket inti tidak memiliki dependensi wajib:
pip install aspose-noteVerifikasi instalasi:
from aspose.note import Document
print("Installation OK")Langkah 2: Muat File .one
Buat instance Document dengan melewatkan jalur file:
from aspose.note import Document
doc = Document("MyNotes.one")
print(f"Section: {doc.DisplayName}")
print(f"Pages: {len(list(doc))}")Untuk memuat dari aliran biner (mis. dari penyimpanan cloud atau respons HTTP):
from aspose.note import Document
with open("MyNotes.one", "rb") as f:
doc = Document(f)Langkah 3: Ekstrak Semua Teks Biasa
Gunakan GetChildNodes(RichText) untuk mengumpulkan setiap node RichText dalam pohon dokumen. Ini melakukan pencarian depth‑first rekursif di semua halaman, outline, dan elemen outline:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
for text in texts:
print(text)Untuk menyimpan semua teks ke file:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
texts = [rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text]
with open("extracted_text.txt", "w", encoding="utf-8") as out:
out.write("\n".join(texts))
print(f"Wrote {len(texts)} text blocks to extracted_text.txt")Langkah 4: Periksa Run yang Diformat
Setiap RichText node berisi daftar TextRuns segmen TextRun. Setiap run membawa TextStyle independen dengan format per‑karakter:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
style = run.Style
attrs = []
if style.IsBold: attrs.append("bold")
if style.IsItalic: attrs.append("italic")
if style.IsUnderline: attrs.append("underline")
if style.IsStrikethrough: attrs.append("strikethrough")
if style.FontName: attrs.append(f"font={style.FontName}")
if style.FontSize: attrs.append(f"size={style.FontSize}pt")
label = ", ".join(attrs) if attrs else "plain"
print(f"[{label}] {run.Text!r}")Langkah 5: Ekstrak Tautan
Hyperlink disimpan pada node TextRun individu. Periksa Style.IsHyperlink dan baca Style.HyperlinkAddress:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
if run.Style.IsHyperlink and run.Style.HyperlinkAddress:
print(f"Link text: {run.Text!r}")
print(f"URL: {run.Style.HyperlinkAddress}")Langkah 6: Ekstrak Teks Per Halaman
Untuk mengekstrak teks yang diatur berdasarkan judul halaman:
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)"
)
print(f"\n=== {title} ===")
for rt in page.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)Masalah Umum dan Solusi
1. ImportError: Tidak ada modul bernama ‘aspose’
Penyebab: Paket tidak terpasang di lingkungan Python yang aktif.
Perbaikan:
pip install aspose-note
##Confirm active environment:
pip show aspose-note2. FileNotFoundError saat memuat file .one
Penyebab: Jalur file tidak benar atau file tidak ada.
Perbaikan: Gunakan jalur absolut atau verifikasi bahwa file ada sebelum memuat:
from pathlib import Path
from aspose.note import Document
path = Path("MyNotes.one")
if not path.exists():
raise FileNotFoundError(f"File not found: {path.resolve()}")
doc = Document(str(path))3. UnicodeEncodeError pada Windows saat mencetak
Penyebab: Terminal Windows mungkin menggunakan enkoding lama yang tidak dapat menampilkan karakter Unicode.
Perbaikan: Konfigurasikan ulang stdout di awal skrip Anda:
import sys
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")4. Hasil teks kosong
Cause: File .one mungkin kosong, hanya berisi gambar atau tabel (tanpa node RichText), atau merupakan file notebook (.onetoc2) bukan file bagian (.one).
Perbaikan: Periksa jumlah halaman dan periksa tipe node:**
from aspose.note import Document
doc = Document("MyNotes.one")
print(f"Pages: {len(list(doc))}")
for page in doc:
print(f" Children: {sum(1 for _ in page)}")5. IncorrectPasswordException
Penyebab: File .one dienkripsi. Dokumen yang dienkripsi tidak didukung.
Fix: Aspose.Note FOSS untuk Python tidak mendukung file terenkripsi .one. Produk komersial Aspose.Note yang lengkap mendukung dekripsi.
Pertanyaan yang Sering Diajukan
Apakah saya dapat mengekstrak teks dari semua halaman sekaligus?
Ya. doc.GetChildNodes(RichText) mencari seluruh pohon dokumen secara rekursif, termasuk semua halaman, outline, dan elemen outline.
Apakah perpustakaan mendukung file notebook .onetoc2?
Tidak. Perpustakaan hanya menangani file bagian .one. File daftar isi notebook (.onetoc2) adalah format yang berbeda dan tidak didukung.
Bisakah saya mengekstrak teks dari tabel?
Ya. TableCell node berisi anak RichText yang dapat dibaca dengan cara yang sama:
from aspose.note import Document, Table, TableRow, TableCell, RichText
doc = Document("MyNotes.one")
for table in doc.GetChildNodes(Table):
for row in table.GetChildNodes(TableRow):
for cell in row.GetChildNodes(TableCell):
cell_text = " ".join(rt.Text for rt in cell.GetChildNodes(RichText)).strip()
print(cell_text, end="\t")
print()Versi Python apa yang didukung?
Python 3.10, 3.11, dan 3.12.
Apakah perpustakaan ini thread-safe?
Setiap instance Document harus digunakan dari satu thread. Untuk ekstraksi paralel, buat Document terpisah per thread.
Sumber Daya Terkait: