RichTextObject module
- class RichTextObject.RichTextObject
Bases:
SpireObject,IRichTextStringRepresents a rich text object that can contain text with multiple formatting runs.
This class implements the IRichTextString interface and provides functionality for creating and manipulating text with different formatting applied to different parts. Rich text objects can be used in cell values, comments, and other text containers in Excel.
- Append(text: str, font: IFont)
Appends text with the specified font to the end of the rich text.
- Parameters:
text (str) – The text to append.
font (IFont) – The font to apply to the appended text.
- BeginUpdate()
Begins a batch update operation on the rich text object.
This method marks the start of a series of changes to the rich text object. Multiple changes can be made more efficiently by calling BeginUpdate before making the changes and EndUpdate after all changes are complete.
- Clear()
Clears both the text content and formatting.
This method removes all text and formatting information, resulting in an empty rich text object.
- ClearFormatting()
Clears all formatting from the rich text.
This method removes all formatting information while preserving the text content, resulting in plain text with default formatting.
- EndUpdate()
Ends a batch update operation on the rich text object.
This method should be called after BeginUpdate and all desired changes have been made. It applies all pending changes to the rich text object.
- GetFont(position: int) XlsFont
Gets the font used at the specified character position in the text.
- Parameters:
position (int) – The zero-based character position in the text.
- Returns:
The font object used at the specified position.
- Return type:
- property IsFormatted: bool
Gets whether the rich text contains any formatting.
- Returns:
- True if the text contains formatting (different fonts, colors, etc.);
otherwise, False.
- Return type:
bool
- property Parent: SpireObject
Gets the parent object that contains this rich text object.
- Returns:
The parent object that contains this rich text object.
- Return type:
SpireObject
- property RtfText: str
Gets the text content in Rich Text Format (RTF).
This property returns the text with all formatting information encoded in RTF format, which can be used for interoperability with other applications that support RTF.
- Returns:
The text content in RTF format.
- Return type:
str
- SetFont(startPos: int, endPos: int, font: IFont)
Sets the font for a range of characters in the text.
- Parameters:
startPos (int) – The zero-based starting character position.
endPos (int) – The zero-based ending character position (inclusive).
font (IFont) – The font to apply to the specified range of characters.
- property Text: str
Gets or sets the plain text content of the rich text object.
When setting this property, any existing formatting is preserved and applied to the new text content where applicable.
- Returns:
The plain text content without formatting information.
- Return type:
str
