Create Visio Diagram in Python

MS Visio is a popular application that enables you to create flowcharts, data flow diagrams, business process models, etc. It uses the VSDX file format to store the diagrams. In certain cases, we may need to create Visio diagrams programmatically. In this article, we will learn how to create a VSDX diagram from scratch in Python. Furthermore, it covers how to add basic shapes in the VSDX diagrams programmatically.

The article shall cover the following topics:

  1. Python Diagram Library to Create Visio Diagram
  2. Create MS Visio Diagram
  3. Create a Shape in Visio Diagram

Python Diagram Library to Create Visio Diagram

To create MS Visio diagrams, we will be using the Aspose.Diagram for Python. It is a feature-rich API that allows creating, editing, converting, and processing MS Visio diagrams. The API makes it easier for you to manipulate the VSDX diagrams with easy-to-use properties and methods.

The Diagram class of the API represents the Root element of the Visio objects hierarchy. The save(string, SaveFileFormat) method of this class saves the diagram data to the file. We have the add_shape(double, double, double, double, string, int) method of the Diagram class that adds the shape with the defined PinX, PinY, Width, and Height. The SaveFileFormat enumeration provides the output diagram format selection.

Please either download the package or install the API from PyPI using the following pip command in the console:

pip install aspose-diagram-python 

Create Visio VSDX Diagram using Python

We can easily create an empty VSDX diagram by following the steps given below:

  • Create an instance of the Diagram class.
  • Use the save() method to save the file as VSDX.

The following code sample shows how to create a Visio VSDX diagram in Python.

Create a Shape in Visio Diagram using Python

Shapes are the building blocks of the Visio diagrams. MS Visio supports a wide range of shapes to create diagrams in various domains. For adding shapes, we need to add the stencil (VSS) that contains a collection of shapes to be used in diagrams. We can create shapes in Visio diagrams by following the steps given below:

  1. Firstly, load an existing VSS file using the Diagram class.
  2. Next, add a new rectangular shape using the add_shape() method.
  3. Then, add some text to the rectangle shape.
  4. Optionally, add more shapes such as circles, etc.
  5. Finally, save the VSDX diagram using the save() method.

The following code sample shows how to add a shape to the Visio diagram using Python.

Create a Shape in Visio Diagram using Python

Create a Shape in Visio Diagram using Python.

Get a Free License

You can get a free temporary license to try the library without evaluation limitations.

Conclusion

In this article, we have learned how to:

  • create a new VSDX diagram in Python;
  • load a Visio template file;
  • add a rectangular shape to the Visio diagram;
  • add a circle to the VSDX in Python.

Besides creating a Visio diagram in Python, you can learn more about Aspose.Diagram for Python API using the documentation. In case of any ambiguity, please feel free to contact us at our free support forum.

See Also