The release of Python Tools for Visio Studio means that we know have a much better experience developing against my favorite application. In the interests of showing off how good this is and in preparation for some exciting work I have planned around Infographics on my other blog, I’m starting a small series of blog posts on and screencasts to demonstrate how this all works.

 

INSTALLATION

Install IronPython 2.7 (or later). Make sure you DO NOT INSTALL the “Tools for Visual Studio” component.

image

Then install Python Tools for Visio Studio 2010

To verify you have Python Tools installed just start the Extension Manager

image

And look for these items that are highlighted under Installed Extensions

image

 

Start a new project via File > New > Project

image

 

Under Installed Templates > Other Languages > Python select IronPython Application and click OK

 

image

 

You can see the project in the Solution Explorer and it has a simple “hello world” project aready there

image

 

Click the green arrow to start debugging.

image

And you’ll of course, see the code run. The console window will show “hello world” and go away quickly.

image

 

To start, we want the console to stay around and wait for a keypress. This will be convenient for my future blog posts, so let’s take care of that first.

As you start trying to import you’ll notice the first nice feature: Intellisense! Import the clr and System modules.

 

image   image

 

And now call System.Console.Readkey – clearly Intellisense is hooked up for more than just imports.

 

image

 

CONNECTING TO VISIO

Now we have to get a reference to Visio. Fortunately this is very easy. we just use clr.AddReference, do an import, and finally assign a quick alias so that we can just type “IVisio” instead of “Microsoft.Office.Interop.Visio”

clr.AddReference("Microsoft.Office.Interop.Visio")
import Microsoft.Office.Interop.Visio
IVisio = Microsoft.Office.Interop.Visio

 

image

 

And look, Intellisense is still working here.

image

 

Now lets do some basic Visio operations. We create the application, add a new document, get a reference to the active page and start drawing.

visapp = IVisio.ApplicationClass()
doc = visapp.Documents.Add("")
page = visapp.ActivePage
shape = page.DrawRectangle(1, 1, 5, 4)
shape.Text = "Hello World" 

 

image

 

Now run the app, and everything works. In this case, I have Visio 2010 installed on my machine so that is what launched. The procedure and code would work with Visio 2007

image

Success!!!

 

FULL SCRIPT

import clr
import System
clr.AddReference("Microsoft.Office.Interop.Visio")
import Microsoft.Office.Interop.Visio
IVisio = Microsoft.Office.Interop.Visio

print('Hello world')

visapp = IVisio.ApplicationClass()
doc = visapp.Documents.Add("")
page = visapp.ActivePage
shape = page.DrawRectangle(1, 1, 5, 4)
shape.Text = "Hello World"

 

WHAT’S NEXT

In this post, I focused on creating a project and a script, the next time, I’ll cover connecting to Visio via the IronPython Interactive shell that is part of Python Tools for Visio Studio 2010