-1

I want to have my python program generate visio drawings using shapes from a stencil (.vss) file. How can I do that? I think I could generate the xml formatted .vdx file, but there isn't a lot of documentation on the .vdx format.

EDIT: the computer has visio installed.

user2482876
  • 288
  • 4
  • 15
  • What do you mean by 'manually'? Without visio installed? – Nikolay Nov 03 '15 at 06:21
  • Yeah, I guess my question is unclear. My goal is to write some code that will generate a visio drawing. I figured i could write code that would generate the xml formatted vdx file, but there isn't a lot of documentation on the vdx format. – user2482876 Nov 03 '15 at 14:13
  • I edited the question to show better what i want to do. – user2482876 Nov 03 '15 at 14:37
  • It's still not quite clear - do you have Visio installed (on the computer where you want to generate the vdx) or not? This is important, because if you have Visio installed then you can use it to do things for you; if you don't then your task is next to impossible. – Nikolay Nov 03 '15 at 14:42
  • yes, the computer has Visio installed, But I would like to generate drawings from code and the only language I know is Python and most of my scripts from which I would like to generate drawings are in python. – user2482876 Nov 03 '15 at 14:47

1 Answers1

1

If you have Visio installed, then you can use Visio API and Python CLR or COM bindings to make it do the things for you. Here are some similar SO questions (visio and python):

Reading the contents of Microsoft Visio (2010) doc in IronPython

Cannot open Visio document with Python

Check out Visio SDK and the free "Developing Visio Solutions" book in MSDN to start with.

Anyways, some code to start with (opens a standard "basic shapes" .VSS stencil then drops a rectangle shape and then saves as .VDX):

import win32com.client

visio = win32com.client.Dispatch("Visio.Application")
doc = visio.Documents.Add("")
stn = visio.Documents.Open("BASIC_M.VSS")

page = doc.Pages.Item(1)

master = stn.Masters.Item("Rectangle")
rect = page.Drop(master, 0, 0)

doc.SaveAs("C:\\<some directory>\\file.vdx")
doc.Close()

visio.Quit()
Community
  • 1
  • 1
Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • `import wind32com.client` doesn't work for me.I used: `import sys import clr import System clr.AddReference("Microsoft.Office.Interop.Visio") import Microsoft.Office.Interop.Visio IVisio = Microsoft.Office.Interop.Visio visio = IVisio.ApplicationClass()` as per http://stackoverflow.com/questions/27439603/reading-the-contents-of-microsoft-visio-2010-doc-in-ironpython – user2482876 Nov 03 '15 at 17:32
  • To get that (win32com) you may need to install python for windows extensions. But if it's already working the other way that's fine :) – Nikolay Nov 03 '15 at 17:45
  • Would you know where to find the documentation on `win32com.client.Dispatch("Visio.Application")`? I am trying to understand how to connect shapes and edit the shape data. BTW, thanks for your help! – user2482876 Nov 03 '15 at 18:08
  • In Visio SDK, there are a lot of samples.. and the "Developing Visio Solutions" book is of a good help for starters. Check out also MSDN articles: https://msdn.microsoft.com/en-us/library/office/ff724002.aspx – Nikolay Nov 03 '15 at 19:24
  • Ok, thanks!, Yeah, I am slowly finding my way through the SDK documentation. – user2482876 Nov 03 '15 at 19:36
  • Hi, I am trying to use the same code. At line clr.AddReference("Microsoft.Office.Interop.Visio") I get FileNotFoundException: Unable to find assembly 'Microsoft.Office.Interop.Visio'. error. What should I do? Thanks.. – Ferda-Ozdemir-Sonmez Jan 08 '21 at 13:50
  • @user2482876 did you do anything to eliminate the Unable to find assembly error for the code you shared in the comment? – Ferda-Ozdemir-Sonmez Jan 08 '21 at 14:52
  • @Ferda-Ozdemir-Sonmez probably you have issue with x32 and x64 (example: you have Visio x64 installed but trying to call it from x32 app. You need to have match between Visio and the code that is calling it, like both 32 bits or both 64 bits). – Nikolay Jan 08 '21 at 14:55
  • @Nikolay hi, thanks for the response. Both are 64 bits unfortunetely.. – Ferda-Ozdemir-Sonmez Jan 08 '21 at 20:13
  • @Ferda-Ozdemir-Sonmez As far as I can tell the message that "Microsoft.Office.Visio.Interop" not found (or "Visio.Application" is not registered) could mean that Visio is not installed at all, installed incorrectly, or that the bitness (32/64) is mismatching. I don't know any other reason :) – Nikolay Jan 09 '21 at 17:56
  • @Nikolay thanks for the response.. It is installed but apperantly it can not be seen from Python environment (mine was anaconda). Is there something special that I have to do to introduce Visio to Python environment? – Ferda-Ozdemir-Sonmez Jan 10 '21 at 15:28
  • @Ferda-Ozdemir-Sonmez I'm not aware of any issues except mismatching bitness, sorry. You can check that in Visio's about box, and in the python start prompt (when you type "python") – Nikolay Jan 10 '21 at 17:02