Launch Visual Studio and Under Tools > Python Tools click on IronPython 2.7 Interactive
The window now appears.
The full script we used in the first post in this series works here, of course
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"
You should be aware though that if you simply copy the code above and paste it into the interactive shell window, you’ll get this behavior:
This means that this is all treated as one line and of course that isn’t cool with Python. If you hit RETURN to execute you’ll see this:
If we are going to play around with Visio this way we don’t want to have to enter all the import statements and use clr.AddReference everytime we want to do something quick in Visio.
There are two solutions: use the Send to Interactive feature or use a separate .py file. The Send to Interactive feature allows you to select from text from a .py file in Visual Studio and correctly pushes it to the Interactive shell.
Send to Interactive screenshot:
However, because it will be more convenient for future blog posts, I’ll use the alternate solution of keeping a separate python file. We’ll save this as a file called ironvisio.py and then import that as needed in the interactive shell.
(In the screenshot below I have not saved the file yet so it is still called “TextFile1.txt”)
But where to put the file so we can import it later?
Let’s clean up the Interactive shell …
And then see what paths it is looking at
In this example, I’m going to place the file in “C:\Program Files (x86)\IronPython 2.7\Lib\site-packages” so that I always have access to it.
(Obviously there isn’t much going on in site-packages on my machine)
Oops… (I knew this would happen but wanted to show it)
if I click yes, then it’s going to save in c:\users\saveenr – which isn’t where the interactive shell can find it.
What are my options?
I could try setting the path every time, but I don’t want to have to remember to do this.
Fortunately, all you have to do is set the IRONPYTHONPATH environment variable correctly and restart Visual Studio and your path will include the new folder.
From Start menu >Computer, right-click properties
click on Advanced system settings
Under the Advanced tab, click on Environment Variables
And enter the path as described below and close the Control Panel.
Now restart Visual Studio and launch the interactive shell and look at sys.path.
Good. It’s there.
If you are into powershell, you can use the following commands to set it for the user of the system. Make sure to run with elevated privileges when making this change.
I’ll now save this script into a file called d:\ironvisio\ironvisio.py
import clr import System clr.AddReference("Microsoft.Office.Interop.Visio") import Microsoft.Office.Interop.Visio IVisio = Microsoft.Office.Interop.Visio
Now I’ll do the import.
And start creating the Visio application class.
NOTE: Currently I am seeing that Intellisense is not always working immediately when I import the script inthis wasof the fully-qualified “Microsoft.Office.Interop.Visio”
So for example, this works…
But the following seems to interfere with Intellisense at the start – several minutes later it seems to kick in.
I haven’t sorted out why there is a delay in this case, but it is worth knowing so that you can avoid frustration if you really want a good Intellisense experience.
And now you can use the rest of the script to draw something.
As you can see, putting the initial code into a separate file we can import makes it convenient to get started interactively controlling Visio.
- We’ve taken a first step at using the Interactive shell. In the next post we are going to do some basic data-driven drawing. The eventual goal will be to do some nice Infographics using Visio.