RichText

RichText — Aspose.Note FOSS for Python API Reference

Lớp: RichText

Enumerations: aspose.note Enumerations: from aspose.note import RichText Enumerations: CompositeNode

RichText đại diện cho một khối văn bản có định dạng bên trong một OutlineElement, TableCell, hoặc Title. .Text và các đoạn định dạng riêng lẻ thông qua .Runs.


Enumerations

EnumerationsEnumerationsEnumerationsEnumerations
TextstrEnumerationsChuỗi văn bản thuần đầy đủ (tất cả các đoạn được nối lại)
Runslist[TextRun]EnumerationsDanh sách có thứ tự của các đoạn được định dạng riêng lẻ
FontSize`floatNone`Enumerations
Tagslist[NoteTag]EnumerationsCác thẻ OneNote được gắn vào khối văn bản này

Kế thừa từ CompositeNode / Node

EnumerationsEnumerations
ParentNodeNút chứa (OutlineElement, TableCell, Title)
DocumentTài liệu gốc

Enumerations

Enumerations

rt.Append(text: str, style: TextStyle | None = None) -> RichText

Thêm một đoạn văn bản mới với kiểu tùy chọn. Trả về self để nối chuỗi. Hoạt động trong bộ nhớ; các thay đổi không thể được lưu lại vào .one.

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
first_rt = doc.GetChildNodes(RichText)[0]
first_rt.Append(" [reviewed]")

Replace(old_value, new_value)

rt.Replace(old_value: str, new_value: str) -> None

Thay thế mọi lần xuất hiện của old_value bằng new_value trong tất cả các đoạn. Hoạt động trong bộ nhớ.

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    rt.Replace("TODO", "DONE")

Accept(visitor)

rt.Accept(visitor: DocumentVisitor) -> None

Enumerations VisitRichTextStart(rt)VisitRichTextEnd(rt) trên người truy cập.


TextRun

Mỗi phần tử của RichText.Runs là một TextRun:

EnumerationsEnumerationsEnumerations
Textstrđoạn văn bản
StyleTextStyleThuộc tính định dạng cho đoạn này
Start`intNone`
End`intNone`

TextStyle

TextRun.Style là một TextStyle với các thuộc tính sau:

EnumerationsEnumerationsEnumerations
BoldboolEnumerations
ItalicboolEnumerations
UnderlineboolEnumerations
StrikethroughboolEnumerations
SuperscriptboolEnumerations
SubscriptboolEnumerations
FontName`strNone`
FontSize`floatNone`
FontColor`intNone`
Highlight`intNone`
Language`intNone`
IsHyperlinkboolLiệu đoạn này có phải là liên kết siêu văn bản hay không
HyperlinkAddress`strNone`

Ví dụ sử dụng

Lấy toàn bộ văn bản thuần

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    if rt.Text:
        print(rt.Text)

Kiểm tra tất cả các đoạn có định dạng

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    for run in rt.TextRuns:
        s = run.Style
        attrs = [a for a, v in [
            ("bold", s.IsBold), ("italic", s.IsItalic),
            ("underline", s.IsUnderline), ("strike", s.IsStrikethrough),
        ] if v]
        label = ", ".join(attrs) or "plain"
        print(f"  [{label}] {run.Text!r}")

Trích xuất siêu liên kết

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} -> {run.Style.HyperlinkAddress}")

Tìm tất cả văn bản in đậm

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
bold_segments = [
    run.Text for rt in doc.GetChildNodes(RichText)
    for run in rt.TextRuns
    if run.Style.IsBold and run.Text.strip()
]
print(bold_segments)

Đọc thẻ

from aspose.note import Document, RichText

doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
    for tag in rt.Tags:
        print(f"Tag: {tag.label!r}  on text: {rt.Text.strip()!r}")

None-Safety

Text luôn luôn là một str (không bao giờ None). Runs luôn luôn là một danh sách (có thể rỗng). FontSize, FontColor, Highlight, Language, HyperlinkAddress có thể là None.


Xem Thêm

 Tiếng Việt