Trích xuất Văn bản — Aspose.Note FOSS cho Python
Aspose.Note FOSS for Python exposes the full text content of every OneNote page through the RichText nút. Mỗi RichText chứa cả văn bản thuần .Text chuỗi và một .TextRuns danh sách các đoạn được định dạng riêng lẻ TextRun đoạn. Trang này ghi lại mọi mẫu trích xuất văn bản có sẵn.
Trích xuất toàn bộ văn bản thuần
Cách nhanh nhất để lấy toàn bộ văn bản từ một tài liệu là GetChildNodes(RichText), thực hiện một phép duyệt đệ quy theo chiều sâu trên toàn bộ DOM:
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)Thu thập vào danh sách và nối lại:
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
)Trích xuất văn bản theo trang
Sắp xếp văn bản đã trích xuất theo tiêu đề trang:
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)Kiểm tra các đoạn định dạng
RichText.TextRuns là một danh sách của TextRun đối tượng. Mỗi lần chạy bao phủ một dải ký tự liên tục với một 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}")Tham chiếu thuộc tính TextStyle
| Thuộc tính | Kiểu | Mô tả |
|---|---|---|
IsBold | bool | Văn bản in đậm |
IsItalic | bool | Văn bản in nghiêng |
IsUnderline | bool | Văn bản gạch chân |
IsStrikethrough | bool | Văn bản gạch ngang |
IsSuperscript | bool | Chỉ số trên |
IsSubscript | bool | Chỉ số dưới |
FontName | `str | None` |
FontSize | `float | None` |
FontColor | `int | None` |
Highlight | `int | None` |
Language | `int | None` |
IsHyperlink | bool | Liệu đoạn này có phải là liên kết siêu văn bản hay không |
HyperlinkAddress | `str | None` |
Trích xuất siêu liên kết
Liên kết siêu văn bản được lưu tại TextRun cấp độ. Kiểm tra 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}")Trích xuất văn bản in đậm và được tô sáng
Lọc các đoạn theo thuộc tính định dạng để cô lập nội dung cụ thể:
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}")Trích xuất văn bản từ các khối tiêu đề
Tiêu đề trang là RichText các nút bên trong Title đối tượng. Chúng không được trả về bởi một cấp cao nhất GetChildNodes(RichText) trên trang trừ khi bạn bao gồm Title cây con. Truy cập chúng trực tiếp:
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)Trích xuất văn bản từ bảng
Các ô bảng chứa RichText các phần tử con. Sử dụng lồng nhau GetChildNodes các lời gọi:
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)Các thao tác văn bản trong bộ nhớ
Thay thế văn bản
RichText.Replace(old_value, new_value) thay thế văn bản trong bộ nhớ trên mọi lần chạy:
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 supportedThêm một run văn bản
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 exampleLưu văn bản đã trích xuất vào tệp
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.")Mẹo
GetChildNodes(RichText)trên mộtDocumenttìm kiếm toàn bộ cây bao gồm tất cả các trang, đề mục và các phần tử đề mục. Gọi nó trên một cụ thểPageđể giới hạn phạm vi.- Luôn kiểm tra
rt.Text(hoặcif rt.Text:) trước khi in, vì các nút trống tồn tại trong một số tài liệu.RichTextcác nút tồn tại trong một số tài liệu. - Trên Windows, cấu hình lại
sys.stdoutthành UTF-8 để tránhUnicodeEncodeErrorkhi in các ký tự nằm ngoài trang mã hệ thống. TextRunchỉ cóTextvàStylecác trường. Không cóStart/Endthuộc tính offset; để xác định vị trí văn bản của một run trong phần tử chaRichText.Text, tìm kiếmrun.Texttrongrt.Textbằng tay.