This is an independent community resource about Dia. We are not affiliated with the GNOME Dia project. PyDia documentation is sparse — the examples below are minimal, tested patterns; adapt property names to your Dia build.

Automate Dia with Python Scripting

Dia embeds a Python interpreter (“PyDia”). That means repetitive diagram chores — exporting every layer, renaming dozens of objects, generating repetitive drawings — can be scripted instead of clicked. This page gives you the smallest useful starting points.

Expectations, set honestly: PyDia’s documentation is thin and its API surface is small compared to modern plugin systems. It is excellent for batch chores on your own diagrams; it is not a platform for building polished extensions. Everything below sticks to patterns known to work.

Opening the Python Console

In Dia: Tools > Python Console (menu location may vary slightly by version — look under Tools or Plugins). A console window opens with the dia module pre-imported, bound to the currently active diagram.

Example 1: List Layers and Objects

import dia
d = dia.active_display().diagram
for layer in d.layers:
    print("Layer:", layer.name, "-", len(layer.objects), "objects")

Run this with a diagram open. If it prints your layers, scripting works in your build.

Example 2: Batch-Export Every Layer as PNG

import dia
d = dia.active_display().diagram
for layer in d.layers:
    # hide all layers, then show only this one
    for l in d.layers:
        l.visible = (l is layer)
    d.export("/tmp/layer-%s.png" % layer.name)
# restore visibility
for l in d.layers:
    l.visible = True
d.flush()

This is the classic use case: one diagram with per-customer/per-version layers, exported to separate files automatically.

Example 3: Rename Objects in Bulk

import dia
d = dia.active_display().diagram
n = 0
for layer in d.layers:
    for obj in layer.objects:
        props = obj.properties
        if "name" in props:
            props["name"].value = "node-%02d" % n
            n += 1
d.flush()
Property names differ per object type. Inspect obj.properties.keys() in the console to see what your selected object type exposes — never assume a property exists.

Running Scripts Non-Interactively

Dia can run a script file at startup, which pairs with command-line export for fully automated pipelines: generate or modify diagrams in Python, then export without touching the GUI.

Where to Go Next

  • The PyDia API is documented mainly in the Dia source tree and scattered wiki pages — search for “PyDia” plus your specific need.
  • For one-off automation, the console is enough. For anything bigger, consider whether a code-first tool like PlantUML fits the workflow better — honest advice, not a sales pitch.

Script Your Diagrams

Python scripting ships with every Dia install — free, as always.

Download Dia

Frequently Asked Questions

Which Python version does Dia use?

It depends on the build — typically the system Python 2 historically, with newer source builds on Python 3. Check import sys; sys.version in the console to see yours.

Can Python create new diagrams from scratch?

Barely — the API is oriented toward manipulating open diagrams, not authoring complex new ones. For generated diagrams, text-based tools are usually more productive.

Why is there so little documentation?

PyDia was always a power-user feature with a small audience. The examples on this page cover the patterns the community actually uses.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *