Pengekstrakan Teks — Aspose.Note FOSS untuk Python
Aspose.Note FOSS for Python exposes the full text content of every OneNote page through the RichText node. Setiap RichText menyimpan kedua-dua plain-text .Text string dan satu .TextRuns senarai segmen yang bergaya secara individu TextRun segmen. Halaman ini mendokumentasikan setiap corak pengekstrakan teks yang tersedia.
Ekstrak Semua Teks Biasa
Cara paling cepat untuk mendapatkan semua teks daripada dokumen ialah GetChildNodes(RichText), yang melakukan penelusuran rekursif secara depth-first merentasi seluruh DOM:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)Kumpulkan ke dalam senarai dan gabungkan:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
all_text = "\n".join(
rt.Text for rt in doc.GetChildNodes(RichText) if rt.Text
)Ekstrak Teks Mengikut Halaman
Susun teks yang diekstrak mengikut tajuk 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)Periksa Jalur Pemformatan
RichText.TextRuns adalah senarai TextRun objek. Setiap run meliputi julat berterusan aksara dengan seragam TextStyle:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
style = run.Style
parts = []
if style.IsBold: parts.append("bold")
if style.IsItalic: parts.append("italic")
if style.IsUnderline: parts.append("underline")
if style.IsStrikethrough: parts.append("strikethrough")
if style.IsSuperscript: parts.append("superscript")
if style.IsSubscript: parts.append("subscript")
if style.FontName: parts.append(f"font={style.FontName!r}")
if style.FontSize: parts.append(f"size={style.FontSize}pt")
label = ", ".join(parts) if parts else "plain"
print(f"[{label}] {run.Text!r}")Rujukan Sifat TextStyle
| Sifat | Jenis | Keterangan |
|---|---|---|
IsBold | bool | Teks tebal |
IsItalic | bool | Teks condong |
IsUnderline | bool | Teks bergaris bawah |
IsStrikethrough | bool | Teks coret |
IsSuperscript | bool | Superskrip |
IsSubscript | bool | Subskrip |
FontName | `str | None` |
FontSize | `float | None` |
FontColor | `int | None` |
Highlight | `int | None` |
Language | `int | None` |
IsHyperlink | bool | Sama ada run ini merupakan pautan hiper |
HyperlinkAddress | `str | None` |
Ekstrak Pautan Hiper
Pautan hiper disimpan pada TextRun tahap. Semak Style.IsHyperlink:
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" {run.Text!r:40s} -> {run.Style.HyperlinkAddress}")Ekstrak Teks Tebal dan Disorot
Tapis jalur mengikut sifat pemformatan untuk mengasingkan kandungan tertentu:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
print("=== Bold segments ===")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
if run.Style.IsBold and run.Text.strip():
print(f" {run.Text.strip()!r}")
print("\n=== Highlighted segments ===")
for rt in doc.GetChildNodes(RichText):
for run in rt.TextRuns:
if run.Style.Highlight is not None and run.Text.strip():
color = f"#{run.Style.Highlight & 0xFFFFFF:06X}"
print(f" [{color}] {run.Text.strip()!r}")Ekstrak Teks daripada Blok Tajuk
Tajuk halaman adalah RichText nod dalam Title objek. Mereka tidak dikembalikan oleh peringkat atas GetChildNodes(RichText) pada halaman melainkan anda menyertakan Title subpokok. Akses mereka secara langsung:
from aspose.note import Document, Page
doc = Document("MyNotes.one")
for page in doc.GetChildNodes(Page):
if page.Title:
if page.Title.TitleText:
print("Title text:", page.Title.TitleText.Text)
if page.Title.TitleDate:
print("Title date:", page.Title.TitleDate.Text)
if page.Title.TitleTime:
print("Title time:", page.Title.TitleTime.Text)Ekstrak Teks daripada Jadual
Sel jadual mengandungi RichText anak. Gunakan bersarang GetChildNodes panggilan:
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):
row_values = []
for cell in row.GetChildNodes(TableCell):
cell_text = " ".join(
rt.Text for rt in cell.GetChildNodes(RichText)
).strip()
row_values.append(cell_text)
print(row_values)Operasi Teks Dalam Memori
Ganti teks
RichText.Replace(old_value, new_value) menggantikan teks dalam memori merentasi semua pelaksanaan:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
rt.Replace("TODO", "DONE")
##Changes are in-memory only; saving back to .one is not supportedTambah satu run teks
from aspose.note import Document, RichText, TextStyle
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
rt.Append(" [reviewed]") # appends with default style
break # just the first node in this exampleSimpan Teks yang Dikeluarkan ke Fail
import sys
from aspose.note import Document, RichText
if hasattr(sys.stdout, "reconfigure"):
sys.stdout.reconfigure(encoding="utf-8", errors="replace")
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"Extracted {len(lines)} text blocks.")Petua
GetChildNodes(RichText)pada satuDocumentmencari seluruh pokok termasuk semua halaman, rangka, dan elemen rangka. Panggil ia pada sesuatu yang khususPageuntuk mengehadkan skop.- Sentiasa semak
rt.Text(atauif rt.Text:) sebelum mencetak, kerana nod kosong wujud dalam beberapa dokumen.RichTextnod wujud dalam beberapa dokumen. - Pada Windows, konfigurasikan semula
sys.stdoutkepada UTF-8 untuk mengelakkanUnicodeEncodeErrorapabila mencetak aksara di luar halaman kod sistem. TextRunhanya mempunyaiTextdanStylemedan. TiadaStart/Endsifat offset; untuk mencari teks run dalam indukRichText.Text, carirun.Textdalamrt.Textsecara manual.