Paper Office
paper-pptxAPI referencetext.text

TextFrame

paper-pptx 0.2.0 API reference

The part of a shape that contains its text.

Not all shapes have a text frame. Corresponds to the p:txBody element that can appear as a child element of p:sp. Not intended to be constructed directly.

TextFrame(txBody: CT_TextBody, parent: ProvidesPart)

Attributes

auto_size

attributeauto_sizeMSO_AUTO_SIZE | None

Resizing strategy used to fit text within this shape.

Determins the type of automatic resizing used to fit the text of this shape within its bounding box when the text would otherwise extend beyond the shape boundaries. May be None, MSO_AUTO_SIZE.NONE, MSO_AUTO_SIZE.SHAPE_TO_FIT_TEXT, or MSO_AUTO_SIZE.TEXT_TO_FIT_SHAPE.

font_scale

attributefont_scalefloat | None

Font scale percent of this frame's a:normAutofit, e.g. 62.5 (paper-pptx addition).

100.0 when the frame has a:normAutofit with no explicit scale; None when the frame's autofit setting is anything other than a:normAutofit. Read-only: PowerPoint owns this value (it records the shrink-to-fit reduction last applied); use :meth:normalize_autofit to freeze it into explicit sizes.

line_space_reduction

attributeline_space_reductionfloat | None

Line-spacing reduction percent of a:normAutofit, e.g. 20.0 (paper-pptx addition).

0.0 when the frame has a:normAutofit with no explicit reduction; None when the frame's autofit setting is anything other than a:normAutofit. Read-only.

margin_bottom

attributemargin_bottomLength

Length value representing the inset of text from the bottom text frame border.

:meth:pptx.util.Inches provides a convenient way of setting the value, e.g. text_frame.margin_bottom = Inches(0.05).

margin_left

attributemargin_leftLength

Inset of text from left text frame border as Length value.

margin_right

attributemargin_rightLength

Inset of text from right text frame border as Length value.

margin_top

attributemargin_topLength

Inset of text from top text frame border as Length value.

paragraphs

attributeparagraphstuple[_Paragraph, ...]

Sequence of paragraphs in this text frame.

A text frame always contains at least one paragraph.

part

attributepartXmlPart

The package part containing this object.

text

attributetextstr

All text in this text-frame as a single string.

Read/write. The return value contains all text in this text-frame. A line-feed character ("\n") separates the text for each paragraph. A vertical-tab character ("\v") appears for each line break (aka. soft carriage-return) encountered.

The vertical-tab character is how PowerPoint represents a soft carriage return in clipboard text, which is why that encoding was chosen.

Assignment replaces all text in the text frame. A new paragraph is added for each line-feed character ("\n") encountered. A line-break (soft carriage-return) is inserted for each vertical-tab character ("\v") encountered.

Any control character other than newline, tab, or vertical-tab are escaped as plain-text like "x001B" (for ESC (ASCII 32) in this example).

vertical_anchor

attributevertical_anchorMSO_VERTICAL_ANCHOR | None

Represents the vertical alignment of text in this text frame.

None indicates the effective value should be inherited from this object's style hierarchy.

word_wrap

attributeword_wrapbool | None

True when lines of text in this shape are wrapped to fit within the shape's width.

Read-write. Valid values are True, False, or None. True and False turn word wrap on and off, respectively. Assigning None to word wrap causes any word wrap setting to be removed from the text frame, causing it to inherit this setting from its style hierarchy.

Functions

add_paragraph

funcadd_paragraph()

Return new _Paragraph instance appended to the sequence of paragraphs contained in this text frame.

clear

funcclear()

Remove all paragraphs except one empty one.

fit_text

funcfit_text(font_family: str = 'Calibri', max_size: int = 18, bold: bool = False, italic: bool = False, font_file: str | None = None)

Fit text-frame text entirely within bounds of its shape.

Make the text in this text frame fit entirely within the bounds of its shape by setting word wrap on and applying the "best-fit" font size to all the text it contains.

:attr:TextFrame.auto_size is set to :attr:MSO_AUTO_SIZE.NONE. The font size will not be set larger than max_size points. If the path to a matching TrueType font is provided as font_file, that font file will be used for the font metrics. If font_file is None, best efforts are made to locate a font file with matchhing font_family, bold, and italic installed on the current system (usually succeeds if the font is installed).

paramfont_familystr
= 'Calibri'
parammax_sizeint
= 18
paramboldbool
= False
paramitalicbool
= False
paramfont_filestr | None
= None

normalize_autofit

funcnormalize_autofit(*, min_font_size: Length | None = None, resolve: bool = False) -> None

Freeze this frame's rendered text metrics and set explicit no-autofit.

paper-pptx addition. What the reader currently sees is made explicit, then the frame's autofit is set to a:noAutofit:

  • a:normAutofit with a font scale: every explicit font size in the frame (run, paragraph-default, and end-paragraph properties) is multiplied by the scale. If any run's size is not locally resolvable (neither its own sz nor its paragraph's default), UnsupportedStructureError is raised — unless resolve=True, in which case the size is resolved through the effective-style walk (placeholder → layout → master → theme) and frozen from the resolved value; what the walk cannot resolve still refuses. This API never silently guesses.
  • a:normAutofit with a line-spacing reduction: every paragraph's explicit line spacing is reduced accordingly; any paragraph without explicit line spacing raises UnsupportedStructureError (resolve covers font sizes only in this version).
  • a:spAutoFit, a:noAutofit, or no autofit element: no text metrics change.

min_font_size (a Length, e.g. Pt(11)) is applied after freezing: explicit sizes below the floor are raised to it. Validation completes fully before the first write (a refusal leaves the frame byte-identical).

parammin_font_sizeLength | None
= None
paramresolvebool
= False

Returns

None

On this page