Text

The single line TEXT entity (DXF Reference). The style attribute stores the associated Textstyle entity as string, which defines the basic font properties. The text size is stored as cap height in the height attribute in drawing units. Text alignments are defined as enums of type ezdxf.enums.TextEntityAlignment.

See also

See the documentation for the Textstyle class to understand the limitations of text representation in the DXF format.

Tutorial for Text

Subclass of

ezdxf.entities.DXFGraphic

DXF type

'TEXT'

Factory function

ezdxf.layouts.BaseLayout.add_text()

Inherited DXF attributes

Common graphical DXF attributes

Warning

Do not instantiate entity classes by yourself - always use the provided factory functions!

class ezdxf.entities.Text
dxf.text

Text content as string.

dxf.insert

First alignment point of text (2D/3D Point in OCS), relevant for the adjustments LEFT, ALIGNED and FIT.

dxf.align_point

The main alignment point of text (2D/3D Point in OCS), if the alignment is anything else than LEFT, or the second alignment point for the ALIGNED and FIT alignments.

dxf.height

Text height in drawing units as float value, the default value is 1.

dxf.rotation

Text rotation in degrees as float value, the default value is 0.

dxf.oblique

Text oblique angle (slanting) in degrees as float vlaue, the default value is 0 (straight vertical text).

dxf.style

Textstyle name as case insensitive string, the default value is “Standard”

dxf.width

Width scale factor as float value, the default value is 1.

dxf.halign

Horizontal alignment flag as int value, use the set_placement() and get_align_enum() methods to handle text alignment, the default value is 0.

0

Left

2

Right

3

Aligned (if vertical alignment = 0)

4

Middle (if vertical alignment = 0)

5

Fit (if vertical alignment = 0)

dxf.valign

Vertical alignment flag as int value, use the set_placement() and get_align_enum() methods to handle text alignment, the default value is 0.

0

Baseline

1

Bottom

2

Middle

3

Top

dxf.text_generation_flag

Text generation flags as int value, use the is_backward and is_upside_down attributes to handle this flags.

2

text is backward (mirrored in X)

4

text is upside down (mirrored in Y)

property is_backward: bool

Get/set text generation flag BACKWARDS, for mirrored text along the x-axis.

property is_upside_down: bool

Get/set text generation flag UPSIDE_DOWN, for mirrored text along the y-axis.

set_placement(p1: Vertex, p2: Vertex = None, align: TextEntityAlignment = None)

Set text alignment and location.

The alignments ALIGNED and FIT are special, they require a second alignment point, the text is aligned on the virtual line between these two points and sits vertically at the base line.

  • ALIGNED: Text is stretched or compressed to fit exactly between p1 and p2 and the text height is also adjusted to preserve height/width ratio.

  • FIT: Text is stretched or compressed to fit exactly between p1 and p2 but only the text width is adjusted, the text height is fixed by the dxf.height attribute.

  • MIDDLE: also a special adjustment, centered text like MIDDLE_CENTER, but vertically centred at the total height of the text.

Parameters
  • p1 – first alignment point as (x, y[, z])

  • p2 – second alignment point as (x, y[, z]), required for ALIGNED and FIT else ignored

  • align – new alignment as enum TextEntityAlignment, None to preserve the existing alignment.

set_pos(p1: Vertex, p2: Vertex = None, align: str = None)

Set text alignment and location. (deprecated)

The alignments “ALIGNED” and “FIT” are special, they require a second alignment point, the text is aligned on the virtual line between these two points and sits vertically at the base line.

  • “ALIGNED”: Text is stretched or compressed to fit exactly between p1 and p2 and the text height is also adjusted to preserve height/width ratio.

  • “FIT”: Text is stretched or compressed to fit exactly between p1 and p2 but only the text width is adjusted, the text height is fixed by the dxf.height attribute.

  • “MIDDLE”: also a special adjustment, centered text like “MIDDLE_CENTER”, but vertically centred at the total height of the text.

Warning

Will be removed in v1.0.0, use set_placement()

Parameters
  • p1 – first alignment point as (x, y[, z])

  • p2 – second alignment point as (x, y[, z]), required for “ALIGNED” and “FIT” else ignored

  • align – new alignment as string or None to preserve the existing alignment.

get_placement() Tuple[TextEntityAlignment, Vec3, Optional[Vec3]]

Returns a tuple (align, p1, p2), align is the alignment enum TextEntityAlignment, p1 is the alignment point, p2 is only relevant if align is ALIGNED or FIT, otherwise it is None.

get_pos() Tuple[str, Vec3, Optional[Vec3]]

Returns a tuple (align, p1, p2), align is the alignment method, p1 is the alignment point, p2 is only relevant if align is “ALIGNED” or “FIT”, otherwise it is None (deprecated).

Warning

Will be removed in v1.0.0, use get_placement()

get_align_enum() ezdxf.enums.TextEntityAlignment

Returns the current text alignment as TextEntityAlignment, see also set_placement().

get_align() str

Returns the current text alignment as string (deprecated).

Warning

Will be removed in v1.0.0, use get_align_enum()

set_align_enum(align=TextEntityAlignment.LEFT) Text

Just for experts: Sets the text alignment without setting the alignment points, set adjustment points attr:dxf.insert and dxf.align_point manually.

Parameters

alignTextEntityAlignment

set_align(align: str = 'LEFT') Text

Set the text alignment as string (deprecated)

Warning

Will be removed in v1.0.0, use set_align_enum()

transform(m: Matrix44) Text

Transform the TEXT entity by transformation matrix m inplace.

translate(dx: float, dy: float, dz: float) Text

Optimized TEXT/ATTRIB/ATTDEF translation about dx in x-axis, dy in y-axis and dz in z-axis, returns self.

plain_text() str

Returns text content without formatting codes.

font_name() str

Returns the font name of the associated Textstyle.

fit_length() float

Returns the text length for alignments TextEntityAlignment.FIT and TextEntityAlignment.ALIGNED, defined by the distance from the insertion point to the align point or 0 for all other alignments.