TextRun — Aspose.Note FOSS for Python API Reference

类:TextRun

Properties: aspose.note Properties: from aspose.note import TextRun Properties: Node

TextRun 表示在 a 中共享统一格式的单个连续文本段 RichText 块。A RichText.TextRuns 列表包含一个或多个 TextRun 对象;每个都有其自己的 Style (TextStyle)。这是文档模型中格式化文本的最细粒度单元。.


Properties

PropertiesPropertiesPropertiesProperties
Textstr读/写此段的文本内容。默认值为 "".
StyleTextStyle读/写此段的格式属性。参见 TextStyle.
Start`intNone`Properties
End`intNone`Properties

继承自 Node

PropertiesPropertiesProperties
ParentNode`NodeNone`
Document`DocumentNone`

与 RichText 的关系

Properties RichText 节点拥有一个列表,包含 TextRun 对象,通过 RichText.TextRuns. 串联 run.Text 对所有运行产生完整的 RichText.Text:

RichText.Text == "".join(run.Text for run in RichText.TextRuns)

每次运行携带独立的 Style,,因此单个段落可以包含混合的粗体、斜体、超链接或不同颜色的片段。.


使用示例

检查文档中的所有 run

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 = []
        if s.Bold:        attrs.append("bold")
        if s.Italic:      attrs.append("italic")
        if s.Underline:   attrs.append("underline")
        if s.IsHyperlink: attrs.append(f"link={s.HyperlinkAddress!r}")
        label = ", ".join(attrs) or "plain"
        print(f"[{label}] {run.Text!r}")

查找所有超链接 run

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}")

None 安全性

Text 始终是一个 str (永不 None;;默认是 ""). Start 并且 End 可能是 None 用于在程序化创建的运行中,在它们被附加到 RichText. Style 始终存在。.


另见

 中文