RichText — Aspose.Note FOSS for Python API Reference
类:RichText
Properties: aspose.note Properties: from aspose.note import RichText Properties: CompositeNode
RichText 表示一个位于…中的样式化文本块 OutlineElement, TableCell,,或 Title.。它通过…公开完整的纯文本内容 .Text 并通过…提供单独的格式化段落 .Runs.
Properties
| Properties | Properties | Properties | Properties |
|---|---|---|---|
Text | str | Properties | 完整的纯文本字符串(所有运行串联) |
Runs | list[TextRun] | Properties | 按顺序排列的单独格式化段列表 |
FontSize | `float | None` | Properties |
Tags | list[NoteTag] | Properties | 附加在此文本块上的 OneNote 标签 |
继承自 CompositeNode / Node
| Properties | Properties |
|---|---|
ParentNode | 包含的节点(OutlineElement、TableCell、Title) |
Document | 根文档 |
Properties
Properties
rt.Append(text: str, style: TextStyle | None = None) -> RichText追加一个带可选样式的新文本运行。返回 self 用于链式调用。仅在内存中操作;更改无法保存回 .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替换所有出现的 old_value 为 new_value 跨所有运行。仅在内存中操作。.
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) -> NoneProperties VisitRichTextStart(rt) 和 VisitRichTextEnd(rt) 在访客上。.
TextRun
每个元素的 RichText.Runs 是一个 TextRun:
| Properties | Properties | Properties |
|---|---|---|
Text | str | 段文本 |
Style | TextStyle | 此段的格式属性 |
Start | `int | None` |
End | `int | None` |
TextStyle
TextRun.Style 是一个 TextStyle 具有以下属性::
| Properties | Properties | Properties |
|---|---|---|
Bold | bool | Properties |
Italic | bool | Properties |
Underline | bool | Properties |
Strikethrough | bool | Properties |
Superscript | bool | Properties |
Subscript | bool | Properties |
FontName | `str | None` |
FontSize | `float | None` |
FontColor | `int | None` |
Highlight | `int | None` |
Language | `int | None` |
IsHyperlink | bool | 此运行是否为超链接 |
HyperlinkAddress | `str | None` |
使用示例
获取所有纯文本
from aspose.note import Document, RichText
doc = Document("MyNotes.one")
for rt in doc.GetChildNodes(RichText):
if rt.Text:
print(rt.Text)检查所有带格式的运行
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}")提取超链接
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}")查找所有加粗文本
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)读取标签
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 安全性
Text 始终是一个 str (永不 None). Runs 始终是一个列表(可能为空)。. FontSize, FontColor, Highlight, Language, HyperlinkAddress 可以是 None.
另见
- Properties: RichText 的祖先
- TextRun: 单个格式化段
- TextStyle: 格式属性
- NoteTag: 文本上的标签
- 文本提取开发者指南
- 操作指南:从 OneNote 提取文本 (KB)
- 博客:在 Python 中从 OneNote 提取文本