This topic covers how to create ORG chart in Python. It includes the system settings, stepwise process, and a runnable code sample to create an Organizational chart maker in Python. Furthermore, you can make different customizations to the organizational chart and export the output diagram in VSDX or VSD file format as per your requirements.
Steps to Create ORG Chart in Python
- Set up Aspose.Diagram in your system to create an ORG chart
- Initiate an instance of the Diagram class to load the master shapes from a sample stencil
- Append the required shapes and insert connections between different nodes
- Set various chart properties using the LayoutOptions class and render the output ORG chart
The steps above outline the workflow to develop an ORG chart generator in Python. Firstly, configure the system settings and load the sample master shapes from a stencil file. Subsequently, insert the target shapes and their respective connections to the organizational chart before writing the output diagram.
Code to Create ORG Chart Maker in Python
import aspose.diagram | |
from aspose.diagram import * | |
path = "C://" | |
# Load masters from any existing diagram, stencil or template | |
visioStencil = path + "BasicShapes.vss" | |
rectangleMaster = "Rectangle" | |
connectorMaster = "Dynamic connector" | |
pageNumber = 0 | |
width = 1.0 | |
height = 1.0 | |
pinX = 4.25 | |
pinY = 9.5 | |
# Define values to construct the hierarchy | |
listPos = ["0", "0:0", "0:1", "0:2", "0:3", "0:4", "0:5", "0:6", "0:0:0", "0:0:1", "0:3:0", "0:3:1", "0:3:2", "0:6:0", "0:6:1"] | |
# Define a dictionary to map the string name to long shape id | |
shapeIdMap = {} | |
# Create a new diagram | |
diagram = Diagram(visioStencil) | |
diagram.pages[pageNumber].page_sheet.page_props.page_width.value = 11.0 | |
for orgnode in listPos: | |
# Add a new rectangle shape | |
rectangleId = diagram.add_shape(pinX, pinY, width, height, rectangleMaster, pageNumber) | |
pinX += 1 | |
pinY += 1 | |
# Set the new shape's properties | |
shape = diagram.pages[pageNumber].shapes.get_shape(rectangleId) | |
shape.text.value.add(Txt(orgnode)) | |
shape.name = orgnode | |
shapeIdMap[orgnode] = rectangleId | |
# Create connections between nodes | |
for orgName in listPos: | |
lastColon = orgName.rfind(':') | |
if lastColon > 0: | |
parentName = orgName[:lastColon] | |
shapeId = shapeIdMap[orgName] | |
parentId = shapeIdMap[parentName] | |
connector1 = Shape() | |
connecter1Id = diagram.add_shape(connector1, connectorMaster, pageNumber) | |
diagram.pages[pageNumber].connect_shapes_via_connector(parentId, manipulation.ConnectionPointPlace.RIGHT, | |
shapeId, manipulation.ConnectionPointPlace.LEFT, connecter1Id) | |
# Auto layout CompactTree chart | |
compactTreeOptions = autolayout.LayoutOptions() | |
compactTreeOptions.layout_style = autolayout.LayoutStyle.COMPACT_TREE | |
compactTreeOptions.direction = autolayout.LayoutDirection.DOWN_THEN_RIGHT | |
compactTreeOptions.enlarge_page = False | |
diagram.pages[pageNumber].layout(compactTreeOptions) | |
# Save diagram | |
diagram.save("ORGchart_out.vsdx", SaveFileFormat.VSDX) |
This code snippet showcases the feature to make an ORG chart builder in Python. However, you can improvise it by adjusting the number or order of the shapes, as well as the connector directions, position, etc. Likewise, you can modify the node shapes and connections by manipulating the parent shape ID and other properties to meet your requirements.
This quick guide has covered the information on creating an ORG chart maker in Python. Whereas, if you are interested in creating Visio diagrams from scratch, then read the article on Create Visio Diagram in Python.