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.
Install IronPython 2.7 (or later). Make sure you DO NOT INSTALL the “Tools for Visual Studio” component.
Then install Python Tools for Visio Studio 2010
To verify you have Python Tools installed just start the Extension Manager
And look for these items that are highlighted under Installed Extensions
Start a new project via File > New > Project
Under Installed Templates > Other Languages > Python select IronPython Application and click OK
You can see the project in the Solution Explorer and it has a simple “hello world” project aready there
Click the green arrow to start debugging.
And you’ll of course, see the code run. The console window will show “hello world” and go away quickly.
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.
And now call System.Console.Readkey – clearly Intellisense is hooked up for more than just imports.
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
And look, Intellisense is still working here.
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"
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
IVisio = Microsoft.Office.Interop.Visio
visapp = IVisio.ApplicationClass()
doc = visapp.Documents.Add("")
page = visapp.ActivePage
shape = page.DrawRectangle(1, 1, 5, 4)
shape.Text = "Hello World"
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