A comprehensive Python package for creating, reading, modifying, and writing DXF (Drawing Exchange Format) documents with support for multiple DXF versions.
—
Core functionality for creating, reading, writing, and managing DXF documents. These operations form the foundation of all DXF file manipulation in ezdxf, supporting all DXF versions from R12 through R2018.
from typing import Union, Sequence, TextIO, Optional, List
from pathlib import PathCreate new DXF documents with specified version and setup options.
def new(
dxfversion: str = 'R2013',
setup: Union[str, bool, Sequence[str]] = False,
units: int = 6
) -> Drawing:
"""
Create a new DXF document.
Parameters:
- dxfversion: DXF version ('R12', 'R2000', 'R2004', 'R2007', 'R2010',
'R2013', 'R2018'), default is 'R2013'
- setup: setup default styles - False for no setup, True for all, or
list of topics ['linetypes', 'styles', 'dimstyles', 'visualstyles']
- units: document and modelspace units, default is 6 for meters
Returns:
Drawing: New DXF document instance
"""Usage example:
import ezdxf
# Create new R2018 document with standard setup
doc = ezdxf.new('R2018', setup=True, units=4) # millimeters
# Create minimal R12 document
doc_r12 = ezdxf.new('R12')Read existing DXF documents from various sources including files, streams, and compressed archives.
def read(stream: TextIO, *, errors: str = 'surrogateescape') -> Drawing:
"""
Read DXF document from text stream.
Parameters:
- stream: text stream (file-like object)
- errors: error handling strategy for encoding issues
Returns:
Drawing: DXF document instance
"""
def readfile(
filename: str,
encoding: Optional[str] = None,
errors: str = 'surrogateescape'
) -> Drawing:
"""
Read DXF document from file system.
Parameters:
- filename: path to DXF file
- encoding: text encoding (auto-detected if None)
- errors: error handling strategy for encoding issues
Returns:
Drawing: DXF document instance
"""
def readzip(
zipfile: str,
filename: Optional[str] = None,
*,
errors: str = 'surrogateescape'
) -> Drawing:
"""
Read DXF document from ZIP archive.
Parameters:
- zipfile: path to ZIP file
- filename: DXF file name in ZIP (first .dxf file if None)
- errors: error handling strategy for encoding issues
Returns:
Drawing: DXF document instance
"""
def decode_base64(data: str, *, errors: str = 'surrogateescape'):
"""
Load DXF document from base64 encoded data.
Parameters:
- data: base64 encoded DXF data
- errors: error handling strategy for encoding issues
Returns:
Drawing: DXF document instance
"""Usage examples:
import ezdxf
# Read from file
doc = ezdxf.readfile('drawing.dxf')
# Read from ZIP archive
doc = ezdxf.readzip('archive.zip', 'drawing.dxf')
# Read from stream
with open('drawing.dxf', 'rt') as f:
doc = ezdxf.read(f)
# Read from base64 data
encoded_data = "..." # base64 encoded DXF content
doc = ezdxf.decode_base64(encoded_data)Validate DXF documents and detect structural issues, missing references, and format violations.
def is_dxf_file(filename: str) -> bool:
"""
Check if file is valid DXF format.
Parameters:
- filename: path to file
Returns:
bool: True if valid DXF file
"""
def is_dxf_stream(stream) -> bool:
"""
Check if text stream contains valid DXF data.
Parameters:
- stream: text stream to check
Returns:
bool: True if valid DXF stream
"""Usage examples:
import ezdxf
# Validate file
if ezdxf.is_dxf_file('unknown.dxf'):
doc = ezdxf.readfile('unknown.dxf')
# Validate stream
with open('data.txt', 'rt') as f:
if ezdxf.is_dxf_stream(f):
f.seek(0) # Reset stream position
doc = ezdxf.read(f)Access document metadata, configuration options, and system information.
def print_config(verbose: bool = False, stream = None):
"""
Print ezdxf configuration information.
Parameters:
- verbose: include detailed configuration and environment info
- stream: output stream (stdout if None)
"""
# Configuration objects
options: Options
"""Global configuration options"""
config_files: List[Path]
"""Loaded configuration files"""Usage examples:
import ezdxf
# Print basic configuration
ezdxf.print_config()
# Print detailed configuration
ezdxf.print_config(verbose=True)
# Access configuration options
print(f"Using C extensions: {ezdxf.options.use_c_ext}")
print(f"Test files path: {ezdxf.options.test_files}")Recover corrupted or damaged DXF files with best-effort parsing and error correction.
# From ezdxf.recover module
def read(stream, errors: str = 'ignore'):
"""
Read and recover corrupted DXF document from stream.
Parameters:
- stream: text stream
- errors: error handling ('ignore', 'strict')
Returns:
Drawing: Recovered document
"""
def readfile(filename: str, *, encoding: str = None, errors: str = 'ignore'):
"""
Read and recover corrupted DXF file.
Parameters:
- filename: path to DXF file
- encoding: text encoding
- errors: error handling ('ignore', 'strict')
Returns:
Drawing: Recovered document
"""Usage examples:
import ezdxf.recover
# Attempt to recover corrupted file
try:
doc = ezdxf.recover.readfile('corrupted.dxf')
print("Recovery successful")
except Exception as e:
print(f"Recovery failed: {e}")Save DXF documents to files with various encoding and format options.
class Drawing:
def save(self):
"""Save document to original filename"""
def saveas(self, filename: str, *, encoding: str = 'utf-8', fmt: str = 'asc'):
"""
Save document to specified file.
Parameters:
- filename: target file path
- encoding: text encoding ('utf-8', 'cp1252', etc.)
- fmt: file format ('asc' for ASCII, 'bin' for binary)
"""
def write(self, stream, *, fmt: str = 'asc'):
"""
Write document to stream.
Parameters:
- stream: output stream
- fmt: file format ('asc' for ASCII, 'bin' for binary)
"""Usage examples:
# Save to file
doc.saveas('output.dxf')
# Save with specific encoding
doc.saveas('output.dxf', encoding='cp1252')
# Save as binary DXF (smaller file size)
doc.saveas('output.dxf', fmt='bin')
# Write to stream
with open('output.dxf', 'wt', encoding='utf-8') as f:
doc.write(f)class Drawing:
"""
Main DXF document class containing all drawing data and metadata.
"""
filename: str
dxfversion: str
encoding: str
def modelspace(self) -> Modelspace: ...
def layout(self, name: str = None) -> Layout: ...
def new_layout(self, name: str, dxfattribs: dict = None) -> Layout: ...
class Options:
"""Global configuration options"""
use_c_ext: bool
test_files: str
loaded_config_files: List[Path]
def write(self, stream): ...Install with Tessl CLI
npx tessl i tessl/pypi-ezdxf