Szövegkinyerés — Aspose.Note FOSS a Python számára
Aspose.Note FOSS for Python exposes the full text content of every OneNote page through the RichText csomópont. Minden RichText mind egy egyszerű szöveget .Text karakterláncot és egy .TextRuns listát az egyenként formázott TextRun szegmenseket. Ez az oldal dokumentálja az összes elérhető szövegkinyerési mintát.
Az összes egyszerű szöveg kinyerése
A leggyorsabb módja, hogy egy dokumentumból az összes szöveget kinyerjük, az GetChildNodes(RichText), amely rekurzív mélységi bejárást hajt végre az egész DOM-on:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)Gyűjtsd listába és fűzd össze:
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
)Szöveg kinyerése oldalanként
Rendezd a kinyert szöveget oldalcím szerint:
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)Formázási szegmensek ellenőrzése
RichText.TextRuns egy lista a TextRun objektumok. Minden futás egy folytonos karaktertartományt fed le egységes 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}")TextStyle tulajdonságreferencia
| Tulajdonság | Típus | Leírás |
|---|---|---|
IsBold | bool | Félkövér szöveg |
IsItalic | bool | Dőlt szöveg |
IsUnderline | bool | Aláhúzott szöveg |
IsStrikethrough | bool | Áthúzott szöveg |
IsSuperscript | bool | Felső index |
IsSubscript | bool | Alsó index |
FontName | `str | None` |
FontSize | `float | None` |
FontColor | `int | None` |
Highlight | `int | None` |
Language | `int | None` |
IsHyperlink | bool | Az, hogy ez a futás hiperhivatkozás-e |
HyperlinkAddress | `str | None` |
Hiperhivatkozások kinyerése
A hiperhivatkozások a TextRun szinten tárolódnak. Ellenőrizze 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}")Félkövér és kiemelt szöveg kinyerése
Szűrd a futamokat a formázási tulajdonságok alapján, hogy elkülönítsd a specifikus tartalmat:
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}")Szöveg kinyerése címblokkból
Az oldalcímek RichText csomópontok a Title objektumban. Ezek nem kerülnek visszaadásra egy felső szintű GetChildNodes(RichText) oldalon, hacsak nem tartalmazza a Title alkövet. Hozzáférhet közvetlenül:
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)Szöveg kinyerése táblázatokból
A táblázatcellák tartalmazzák RichText gyermekeket. Használjon beágyazott GetChildNodes hívásokat:
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)Memóriában végzett szövegműveletek
Szöveg cseréje
RichText.Replace(old_value, new_value) helyettesíti a szöveget memóriában minden futtatás során:
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 supportedSzövegrész hozzáfűzése
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 exampleKinyert szöveg mentése fájlba
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.")Tippek
GetChildNodes(RichText)egyDocumentkeresi a teljes fa, amely tartalmazza az összes oldalt, vázlatot és vázlat elemet. Hívja meg egy adottPagea hatókör korlátozásához.- Mindig ellenőrizze
rt.Text(vagyif rt.Text:)RichTextcsomópontok léteznek egyes dokumentumokban. - Windows rendszeren állítsa be újra
sys.stdoutUTF-8-ra, hogy elkerüljeUnicodeEncodeErrora rendszer kódlapján kívül eső karakterek nyomtatásakor. TextRuncsakTextésStylemezőket. NincsenekStart/Endeltolás tulajdonságok; a futás szövegének megtalálásához a szülőbenRichText.Text, keresse meg arun.Textbelülrt.Textmanuálisan.