This tool is meant to explode MTEXT entities into single line TEXT entities by replicating the MTEXT layout as close as possible. This tool requires the optional Matplotlib package to create usable results, nonetheless it also works without Matplotlib, but then uses a mono-spaced replacement font for text size measuring which leads to very inaccurate results.
The supported MTEXT features are:
changing text color
text strokes: underline, overline and strike through
changing text size, width and oblique
changing font faces
stacked text (fractions)
The tool requires an initialized DXF document io implement all these features
by creating additional text styles. When exploding multiple MTEXT entities,
they can share this new text styles. Call the
method just once after all MTEXT entities are processed to create the
required text styles, or use
MTextExplode as context manager by
with statement, see examples below.
There are also many limitations:
A 100% accurate result cannot be achieved.
Character tracking is not supported.
Tabulator stops have only limited support for LEFT and JUSTIFIED aligned paragraphs to support numbered and bullet lists. An excessive use of tabs will lead to incorrect results.
The DISTRIBUTED alignment will be replaced by the JUSTIFIED alignment.
Text flow is always “left to right”.
The line spacing mostly corresponds to the “EXACT” style, except for stacked text (fractions), which corresponds more to the “AT LEAST” style, but not precisely. This behavior maybe will improve in the future.
FIELDS are not evaluated by ezdxf.
MTextExplode(layout, doc=None, spacing_factor=1.0)¶
MTextExplodeclass is a tool to disassemble MTEXT entities into single line TEXT entities and additional LINE entities if required to emulate strokes.
The layout argument defines the target layout for “exploded” parts of the MTEXT entity. Use argument doc if the target layout has no DXF document assigned. The spacing_factor argument is an advanced tuning parameter to scale the size of space chars.
explode(mtext: MText, destroy=True)¶
Explode mtext and destroy the source entity if argument destroy is
Create required text styles. This method is called automatically if the class is used as context manager.
Example to explode all MTEXT entities in the DXF file “mtext.dxf”:
import ezdxf from ezdxf.addons import MTextExplode doc = ezdxf.readfile("mtext.dxf") msp = doc.modelspace() with MTextExplode(msp) as xpl: for mtext in msp.query("MTEXT"): xpl.explode(mtext) doc.saveas("xpl_mtext.dxf")
Explode all MTEXT entities into the block “EXPLODE”:
import ezdxf from ezdxf.addons import MTextExplode doc = ezdxf.readfile("mtext.dxf") msp = doc.modelspace() blk = doc.blocks.new("EXPLODE") with MTextExplode(blk) as xpl: for mtext in msp.query("MTEXT"): xpl.explode(mtext) msp.add_block_ref("EXPLODE", (0, 0)) doc.saveas("xpl_into_block.dxf")