ezdxf - DXF Drawing Manipulation
ezdxf is a comprehensive Python package designed for creating, reading, modifying, and writing DXF (Drawing Exchange Format) documents. It supports a wide range of DXF versions from R12 to R2018 and aims to hide complex DXF details while providing extensive capabilities. The library is actively maintained, with frequent updates for bug fixes and new features, currently at version 1.4.3.
Warnings
- gotcha Naming your Python script 'ezdxf.py' will cause a circular import error (e.g., 'AttributeError: partially initialized module 'ezdxf' has no attribute 'readfile'').
- gotcha Do not directly instantiate DXF entity classes (e.g., `Line(...)`) and try to add them to layouts. Always use the factory methods provided by the layout objects (e.g., `msp.add_line(...)`).
- breaking Saving an existing DXF document to an older DXF version can lead to loss of data and attributes that are not supported in the target older version. ezdxf is not a DXF converter.
- gotcha ezdxf does not support creating or editing ACIS (3DSOLID, REGION, SURFACE) data or OLE objects. These are proprietary formats, and ezdxf only reads/preserves them without modification capabilities.
- gotcha For DXF files with structural flaws or minor corruption, directly using `ezdxf.readfile()` may raise exceptions. The `ezdxf.recover` module provides functions (`recover.readfile()`) to load such files by repairing flaws, but it is a slower process.
- breaking Version 0.10 introduced a complete rewrite of the entity system. While high-level API changes were minimized, users interacting directly with internal tag lists or migrating from very old versions might experience breaking changes. Unknown group codes will be lost if a modified DXF drawing is saved.
Install
-
pip install ezdxf -
pip install ezdxf[draw]
Imports
- ezdxf
import ezdxf
Quickstart
import ezdxf
# 1. Create a new DXF R2010 document
doc = ezdxf.new("R2010")
# 2. Add entities to the modelspace (the main drawing area)
msp = doc.modelspace()
# 3. Add a line from (0,0) to (10,0)
msp.add_line((0, 0), (10, 0))
# 4. Add a circle with center (0,0) and radius 5
msp.add_circle((0, 0), 5)
# 5. Save the DXF document
doc.saveas("my_first_drawing.dxf")
print("Created 'my_first_drawing.dxf'")