RichTextShape module

class RichTextShape.RichTextShape

Bases: SpireObject, IRichTextString

Represents rich text within a shape in Excel.

This class implements the IRichTextString interface and provides functionality for creating and manipulating text with different formatting applied to different parts when the text is contained within a shape (like a text box or comment).

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 shape.

This method marks the start of a series of changes to the rich text shape. 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.

property DefaultFont: XlsFont

Gets the default font for this rich text shape.

The default font is used for any portion of the text that doesn’t have specific formatting applied.

Returns:

The default font object for the rich text shape.

Return type:

XlsFont

EndUpdate()

Ends a batch update operation on the rich text shape.

This method should be called after BeginUpdate and all desired changes have been made. It applies all pending changes to the rich text shape.

property FormattingRunsCount: int

Gets the number of formatting runs in the rich text.

A formatting run is a contiguous range of text that has the same formatting applied.

Returns:

The number of formatting runs in the rich text.

Return type:

int

GetFont(index: int) IFont

Gets the font used at the specified character position in the text.

Parameters:

index (int) – The zero-based character position in the text.

Returns:

The font object used at the specified position.

Return type:

IFont

GetSpace(FormattingRunsIndex: int) int

Gets the spacing value for the specified formatting run.

Parameters:

FormattingRunsIndex (int) – The zero-based index of the formatting run.

Returns:

The spacing value for the specified formatting run.

Return type:

int

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 shape object that contains this rich text.

Returns:

The parent shape object that contains this rich text.

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(startIndex: int, endIndex: int, font: IFont)

Sets the font for a range of characters in the text.

Parameters:
  • startIndex (int) – The zero-based starting character position.

  • endIndex (int) – The zero-based ending character position (inclusive).

  • font (IFont) – The font to apply to the specified range of characters.

SetSpace(FormattingRunsIndex: int, SpaceValue: int)

Sets the spacing value for the specified formatting run.

Parameters:
  • FormattingRunsIndex (int) – The zero-based index of the formatting run.

  • SpaceValue (int) – The spacing value to set for the formatting run.

property Text: str

Gets or sets the plain text content of the rich text shape.

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