Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save aspose-com-gists/e51aa660bf995e7715719f8d29102576 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/e51aa660bf995e7715719f8d29102576 to your computer and use it in GitHub Desktop.
Create Company Organizational Chart in Python
# This code sample shows how to create a company organizational chart in a CompactTree style
import aspose.diagram
from aspose.diagram import *
# Load masters from any existing diagram, stencil or template
visioStencil = "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("CompactTreeChart_out.vsdx", SaveFileFormat.VSDX)
# This code sample shows how to create a company organizational chart in a FlowChart style
import aspose.diagram
from aspose.diagram import *
# Load masters from any existing diagram, stencil or template
visioStencil = "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)
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 FlowChart
flowChartOptions = autolayout.LayoutOptions()
flowChartOptions.layout_style = autolayout.LayoutStyle.FLOW_CHART
flowChartOptions.direction = autolayout.LayoutDirection.TOP_TO_BOTTOM
flowChartOptions.enlarge_page = True
diagram.pages[pageNumber].layout(flowChartOptions)
# Save diagram
diagram.save("FlowChart_out.vsdx", SaveFileFormat.VSDX)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment