Text

One line TEXT entity (DXF Reference). Text.dxf.height in drawing units and defaults to 1, but it also depends on the font rendering of the CAD application. Text.dxf.width is a scaling factor, but the DXF reference does not define the base value to scale, in practice the Text.dxf.height is the base value, the effective text width depends on the font defined by Text.dxf.style and the font rendering of the CAD application, especially for proportional fonts, text width calculation is nearly impossible without knowlegde of the used CAD application and their font rendering behavior. This is one reason why the DXF and also DWG file format are not reliable for exchanging exact text layout, they are just reliable for exchanging exact geometry.

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. (str)

dxf.insert

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

dxf.align_point

second alignment point of text (2D/3D Point in OCS), if the justification is anything other than 'LEFT', the second alignment point specify also the first alignment point: (or just the second alignment point for 'ALIGN' and 'FIT')

dxf.height

Text height in drawing units (float); default value is 1

dxf.rotation

Text rotation in degrees (float); default value is 0

dxf.oblique

Text oblique angle in degrees (float); default value is 0 (straight vertical text)

dxf.style

Textstyle name (str); default value is 'Standard'

dxf.width

Width scale factor (float); default value is 1

dxf.halign

Horizontal alignment flag (int), use set_pos() and get_align(); 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 (int), use set_pos() and get_align(); default value is 0

0

Baseline

1

Bottom

2

Middle

3

Top

dxf.text_generation_flag

Text generation flags (int)

2

text is backward (mirrored in X)

4

text is upside down (mirrored in Y)

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

Set text alignment, valid alignments are:

Vertical

Left

Center

Right

Top

TOP_LEFT

TOP_CENTER

TOP_RIGHT

Middle

MIDDLE_LEFT

MIDDLE_CENTER

MIDDLE_RIGHT

Bottom

BOTTOM_LEFT

BOTTOM_CENTER

BOTTOM_RIGHT

Baseline

LEFT

CENTER

RIGHT

Alignments 'ALIGNED' and 'FIT' are special, they require a second alignment point, text is aligned on the virtual line between these two points and has vertical alignment Baseline.

  • '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, but the result is the same as for 'MIDDLE_CENTER'.

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

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

  • align – new alignment, None for preserve existing alignment.

get_pos() → Tuple[str, Vertex, Optional[Vertex]]

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.

get_align() → str

Returns the actual text alignment as string, see also set_pos().

set_align(align: str = '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

align – test alignment, see also set_pos()