Visio Diagrams with Python: Resize to fit

Resize Page to Fit Contents, Set Zoom to Fit Page

When the drawing is ready, the size of the page will be too large or too small – „fit to contents“ might be the solution.

Additionally, I prefer the Zoom of the active Window to be set that it shows the whole drawing.

page.ResizeToFitContents()      #
window = visio.ActiveWindow     #
window.ViewFit=1                #


Visio Diagrams with Python: Colors

24 Colors to choose from.

„Cells“ contain all „Shape-Format“-Properties


# Line-Color
oval2.Cells("LineColor").FormulaForce = 3

# Fill-Color

# Fill-Pattern "0" - no fill

# Fill-Pattern "3"

# yellow background
# light-blue foreground
oval1.Cells("FillPattern").FormulaForce = 3
oval1.Cells("FillBkgnd").FormulaForce = 5
oval1.Cells("FillForegnd").FormulaForce = 7


Visio Diagrams with Python: Connectors

Connect two existing Shapes

The API is somehow weird, since it creates an (Shape-)Object, but it doesn’t return it.

  • you’ll have to fetch the last object in the shapes-List
connector1.Text = "Connector1"
# * color "3" is "light green"
connector1.Cells("LineColor").FormulaForce = 3
# * ConLineRouteExt "2" is "Curved"
connector1.Cells("ConLineRouteExt").FormulaForce = 2


>>> len(shapes)

Visio Diagrams with Python: Stencils

Stencils and Stencil-Files

Stencils are stored in „VSSX“-Files

server_stencils_filename = "C:\Program Files (x86)\Microsoft Office\root\Office16\Visio Content\1031\SERVER_M.VSSX"

#64 means “open hidden”
server_stencils = visio.Documents.OpenEx(server_stencils_filename,64)



Add a FTP-Server to the page

ftp1 = page.Drop(ftp_stencil,3,3)
ftp1.Text = "FTP-Server#1"


>>> len(shapes)

Visio Diagrams with Python using COM

Visualize Data from Python in Visio

I needed to illustrate some data discovered by a python script in Microsoft Visio.

As a starting-point i found two sources of infomation in the Internet:

(1) Python => COM => MS Excel

How to access a Windows COM-Application from Python:

  • MS Excel not Visio
  • but 50% of the information needed

(2) PowerShell => COM => MS Visio

How to access Microsoft Visio using the COM-Interface

  • PowerShell
  • the other 50% needed.

Result: „Python => COM => MS Visio“

Putting both pieces together, will allow a Python-Script to control MS Visio.

(3) MS Visio „Object Model“

Without the original documentation provided by Microsoft: there is _no chance_ to get anything to work.

  • the next 50% 😉

The Basics

(1) Python „PyWin32“

Install „PyWin32“-Library

C:\RH>pip install pywin32
Collecting pywin32
  Downloading pywin32-300-cp38-cp38-win_amd64.whl (9.3 MB)
     |████████████████████████████████| 9.3 MB 2.2 MB/s
Installing collected packages: pywin32
Successfully installed pywin32-300

Access Windows/COM from Python

  • open the Visio-Application
    • „visio“-object: stores a pointer to access application-Level functions
  • add a document
    • „document“-object
  • fetch the active Visio-Page
    • „page“-object
  • fetch the list of all Shapes at this page
    • „shapes“-object
import win32com.client as win32
visio = win32.gencache.EnsureDispatch('Visio.Application')
document = visio.Documents.Add("")
page = visio.ActivePage
shapes = page.Shapes

(2) Explore the Visio-Object-Model


Object-Methods could get discovered using the Python „dir“, notice for example

  • DrawOval
  • DrawRectangle
>>> dir(page)
['AddDataVisualization', 'AddGuide', 'AutoConnectMany', 'AutoSizeDrawing', 'AvoidPageBreaks', 'BoundingBox', 'CLSID', 'CenterDrawing', 'CreateDataVisualizerDiagram', 'CreateSelection', 'Delete', 'DrawArcByThreePoints', 'DrawBezier', 'DrawCircularArc', 'DrawLine', 'DrawNURBS', 'DrawOval', 'DrawPolyline', 'DrawQuarterArc', 'DrawRectangle', 'DrawSpline', 'Drop', 'DropCallout', 'DropConnected', 'DropContainer', 'DropIntoList', 'DropLegend', 'DropLinked', 'DropMany', 'DropManyLinkedU', 'DropManyU', 'Duplicate', 'Export', 'GetCallouts', 'GetContainers', 'GetFormulas', 'GetFormulasU', 'GetResults', 'GetShapesLinkedToData', 'GetShapesLinkedToDataRow', 'GetTheme', 'GetThemeVariant', 'Import', 'InsertFromFile', 'InsertObject', 'Layout', 'LayoutChangeDirection', 'LayoutIncremental', 'LinkShapesToDataRows', 'OpenDrawWindow', 'Paste', 'PasteSpecial', 'PasteToLocation', 'Print', 'PrintTile', 'ResizeToFitContents', 'SetFormulas', 'SetResults', 'SetTheme', 'SetThemeVariant', 'ShapeIDsToUniqueIDs', 'SpatialSearch', 'SplitConnector', 'UniqueIDsToShapeIDs', 'VisualBoundingBox', '_ApplyTypes_', '__call__', '__class__', '__delattr__', '__dict__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattr__', '__getattribute__', '__gt__', '__hash__', '__init__', '__init_subclass__', '__int__', '__iter__', '__le__', '__lt__', '__module__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '__weakref__', '_get_good_object_', '_get_good_single_object_', '_oleobj_', '_prop_map_get_', '_prop_map_put_', 'coclass_clsid', 'old_Paste', 'old_PasteSpecial']


But Object-Parameters won’t and if you don’t want to guess required parameters of Object-Methods, it’s time to bookmark the original documentation provided by Microsoft:

Start drawing


A good starting point might be the „Page“-Object:

Well add „Shapes“ to the „Page“(-Object) using Methods of the Page-Object: or – both Methods will return an „Shape“-Object.

rect1 = page.DrawRectangle(1,1,2,2)
rect2 = page.DrawRectangle(4,4,5,5)
oval1 = page.DrawOval(1,4,2,5)
oval2 = page.DrawOval(4,1,5,2)

Shape Object

Look at – there’s a „Text“-Property:



>>> len(shapes)