Howto

General preconditions:

import ezdxf
dwg = ezdxf.readfile("your_dxf_file.dxf")
modelspace = dwg.modelspace()

Get/Set block reference attributes

Block references (Insert) can have attached attributes (Attrib), these are simple text annotations with an associated tag appended to the block reference.

Iterate over all appended attributes:

blockrefs = modelspace.query('INSERT[name=="Part12"]')  # get all INSERT entities with entity.dxf.name == "Part12"
if len(blockrefs):
    entity = blockrefs[0]  # process first entity found
    for attrib in entity.attribs():
        if attrib.dxf.tag == "diameter":  # identify attribute by tag
            attrib.dxf.text = "17mm"  # change attribute content

Get attribute by tag:

diameter = entity.get_attrib('diameter')
if diameter is not None:
    diameter.dxf.text = "17mm"

Reduce Memory Footprint

Warning

Data compression costs time: memory usage vs run time

Create More Readable DXF Files (DXF Pretty Printer)

DXF files are plain text files, you can open this files with every text editor which handles bigger files. But it is not really easy to get quick the information you want.

Create a more readable HTML file (DXF Pretty Printer):

# on Windows
py -3 -m ezdxf.pp your_dxf_file.dxf

# on Linux/Mac
python3 -m ezdxf.pp your_dxf_file.dxf

This produces a HTML file your_dxf_file.html with a nicer layout than a plain DXF file and DXF handles as links between DXF entities, this simplifies the navigation between the DXF entities.

Since ezdxf v0.8.3, a script called dxfpp will be added to your Python script path:

usage: dxfpp [-h] [-o] [-r] [-x] [-l] FILE [FILE ...]

positional arguments:
  FILE             DXF files pretty print

optional arguments:
  -h, --help       show this help message and exit
  -o, --open       open generated HTML file with the default web browser
  -r, --raw        raw mode - just print tags, no DXF structure interpretation
  -x, --nocompile  don't compile points coordinates into single tags (only in
                   raw mode)
  -l, --legacy     legacy mode - reorders DXF point coordinates

Important

This does not render the graphical content of the DXF file to a HTML canvas element.

Adding New XDATA to Entity

Adding XDATA as list of tuples (group code, value):

dwg.appids.new('YOUR_APP_NAME')  # IMPORTANT: create an APP ID entry

circle = modelspace.add_circle((10, 10), 100)
circle.tags.new_xdata('YOUR_APP_NAME',
                 [
                     (1000, 'your_web_link.org'),
                     (1002, '{'),
                     (1000, 'some text'),
                     (1002, '{'),
                     (1071, 1),
                     (1002, '}'),
                     (1002, '}')
                 ])

For group code meaning see DXF reference section DXF Group Codes in Numerical Order Reference, valid group codes are in the range 1000 - 1071.

A360 Viewer Problems

AutoDesk web service A360 seems to be more picky than the AutoCAD desktop applications, may be it helps to use the latest DXF version supported by ezdxf, which is DXF R2018 (AC1032) in the year of writing this lines (2018).

Show IMAGES/XREFS on Loading in AutoCAD

If you are adding XREFS and IMAGES with relative paths to existing drawings and they do not show up in AutoCAD immediately, change the HEADER variable $PROJECTNAME='' to (not really) solve this problem. The ezdxf templates for DXF R2004 and later have $PROJECTNAME='' as default value.

Thanks to David Booth:

If the filename in the IMAGEDEF contains the full path (absolute in AutoCAD) then it shows on loading, otherwise it won’t display (reports as unreadable) until you manually reload using XREF manager.

A workaround (to show IMAGES on loading) appears to be to save the full file path in the DXF or save it as a DWG.

So far - no solution for showing IMAGES with relative paths on loading.