Visio Guy

Visio Discussions => Programming & Code => Topic started by: PF4VisioGuy on August 21, 2018, 01:42:21 PM

Title: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 21, 2018, 01:42:21 PM
This is related to a previous post and to my efforts to put in a diagram nodes (graph) and their position that was already calculated with Graphviz
Neato is the algo used for this and the result is a .dot file that contain node attributes and their position in the picture (the neato file contains the size of the paper (WxH) and DPI info)

My plan is to use Python and a Windows Com library to create the docs
Two questions:
-do you foresee any problems with this approach?
-what book or tutorials should I read in order to familiariaze myself with the COM model for VIsio?

You might wonder why not VBA. it is because I know Python and I use it to collect the connectivity information and to produce PDFs, DOT files and so on so I want to keep everything together
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Nikolay on August 21, 2018, 04:52:43 PM
Well, it is technically possible to do with python everything you could do with VBA, it will just take much more time.
Also, your solution will run much slower than VBA solution.
But if you do not care about time and money, you can go with python.

Oh, wait. The questions.

Quote-do you foresee any problems with this approach?

- Most probably you will not have auto-complete or parameter information from the python IDE
- You cannot use Visio macro recorder since it generates VBA
- Most code samples are in VBA or C#, so it's hard to find proper examples
- Since python is running out-of-the-process (python.exe) the code will run times slower than VBA
- You may face some troubles if you want to use some non-automation-compatible Visio methods (there are such). Example: SaveAsWeb is not automation-compatible.
https://stackoverflow.com/questions/49944199/save-visio-document-as-html (requires you to generate a proxy using "import comtypes" instead of "import win32com.client")

Quote-what book or tutorials should I read in order to familiariaze myself with the COM model for VIsio?

- The free beginner's book from the Micrsofot is good ("Developing Visio Solutions (http://msdn.microsoft.com/en-us/library/aa245244(office.10).aspx)")
Other than that, look at this topic: http://visguy.com/vgforum/index.php?topic=791.0
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Yacine on August 21, 2018, 06:04:47 PM
I'm actually playing with the same idea - using jupyter notebooks to remote control visio.
I've seen many tutorials demonstrating awesome stuff with excel. It's a very trivial conclusion to try to do the same with visio.

As of my current opinion about these notebooks,
- they bring a convenient way to solve everyday problems step by step,
- they can be saved as to replicate the steps for later usage,
- their functionality can be improved over time, as they are used,
- python is far superior to VBA in matters of OOP, easy and clean coding, ...
- there are so many libraries for so many problems that it will be difficult not to find the right one for yours


These Jupyter notebooks have the potential to de-throne excel as tool of choice for regular engineers ... and for visio users.
My ideas for this project would be to begin implementing a comfortable set of "visio.utils": getAllShapes, getCellValue, set..., selectShapes(where, criteria, ...), and so on.
Speed is absolutely not an issue. Most of the problems I encoubter in everyday practice are related to finding the right solution for a problem. Very few relate to speed issues. When the latter occurs, I can optimize the solution ...  and there are so many ways to do it.

Having an interactive tool would really be a plus for visio.

The forum regulars probably know my shapes manipulations tools and my efforts in creating a generic solution for graphs à la 3D.js. "Jupyter-Visio" could be a more intuitive way to do the same.


I encourage everyone in the forum to investigate Jupyter, Anaconda and JupyterLab.
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 21, 2018, 10:06:25 PM
Totally agree with the speed non-issue, especially if it is for small
I do need to read about these Jupyter notebooks, they are becoming quite popular and I have no clue what they are and how they can be used

Thank you to both of you for the feedback

Yacine...can you please point me to your activity in regards to adding D3.js like functionality to Visio?
I must say that it is on my to do list to learn D3.js, I am fascinated by its beauty and simplicity and elegancy
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 21, 2018, 10:51:02 PM
Here is some old code I have come across while looking for examples

https://technofrustration.blogspot.com/2007/10/using-python-to-automate-visio.html
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Yacine on August 22, 2018, 09:29:47 AM
Quote from: PF4VisioGuy on August 21, 2018, 10:06:25 PM
Yacine...can you please point me to your activity in regards to adding D3.js like functionality to Visio?

http://visguy.com/vgforum/index.php?topic=8018.0
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Nikolay on August 22, 2018, 12:16:48 PM
Wouldn't it be more reasonable to go with C#.. (you don't want VBA, I got it)?

Check out the articles regarding LINQPAD from the John Goldsmith:
http://visualsignals.typepad.co.uk/vislog/2015/12/getting-started-with-c-in-linqpad-with-visio.html
http://visualsignals.typepad.co.uk/vislog/2018/04/using-utildif-in-linqpad-with-visio.html

You will have at least autocomplete (intellisense) and the constants ;)
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Yacine on August 22, 2018, 02:04:07 PM
Nice hint indead.
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 22, 2018, 02:36:19 PM
@NIkolay -sorry but C# is not an option for me, I already have quite a good amount of code in Python so the Win23com route is mandatory for me
and the whole thing should be fairly simple: add shapes to the diagram at the XY coordinates precalculated by Neato (graphviz) and then connect these shapes with connectors as per the DOT file
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Nikolay on August 22, 2018, 06:51:25 PM
Okay, I see. "You are your own worst enemy" :))

Anyways. The python code should be more or less equivalent to the vbscript code; also the number of python examples is above zero.

Creating a new diagram, and placing shapes should not be difficult, you just need to rescale the coordinates properly.

Placing connectors may be (much) more complicated. They are described as SPLINEs in the GraphViz output, as far as I remember (are they not?).
Converting those SPLINEs to Visio NURBs may be a lot of fun. We had a similar topic on SVG ARC (not curve!) conversion, you could check that one out.
http://visguy.com/vgforum/index.php?topic=7408.15 In that case, only Junichi-san could do it. I hope it is not that bad but it's always better to be prepared for the worst :)

Also, in case you want to convert connectors,  you probably want to use fixed connectors (not-auto-routed).

If your diagrams are simple, you can try shape.AutoConnect, it will auto-generate Visio connector but ditch (ignore) all the layout for the corresponding connector done by graphviz.

I'm also wondering, what Microsoft guys could say (https://visio.uservoice.com) regarding feeding the Data Visualizer from a ".dot" file? Too nerdy?
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: vojo on August 22, 2018, 08:45:51 PM
several simple python <=> visio examples...just google "visio python"   and select verbatim.
from what I saw, the trick to pipe the right win32 library into python instance and do a few python lines in your code
to establish  pythin <=> vsio bridge.

you also could go down the path of writing a python program to create an xml file for your drawing then let visio read it in
and go from there.  Problem more lifting to do this
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 22, 2018, 10:12:11 PM
@Nikolay; you do not need to convert the splines, just get their attributes and ignore the coordinates
So if shape1 has X1Y1 coordinates and shape2 has X2Y2 I am thinking to connect these two with a connector for which I set the attributes as per the dot file (if I want a fancy graph)

@vojo yes probably if I want to keep it simple I could use the XML approach... have to see which one is easier
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Nikolay on August 23, 2018, 12:10:08 AM
Quote from: PF4VisioGuy on August 22, 2018, 10:12:11 PM
@Nikolay; you do not need to convert the splines, just get their attributes and ignore the coordinates

This is the "shape.AutoConnect" approach I mentioned... Sure you can give it a try!
My concern is that sometimes Visio may be not that good at guessing the routes, so a relatively complex diagram can turn into a "crow's nest".
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 23, 2018, 01:47:04 AM
I am ok with having to rearange the shapes after I put them on the page and connect them with sticky connectors.
Neato (Graphwiz) is not always successful in finding the best layout...

Have you guys came across of any VBA example that does this...: creates the doc, adds two shapes and connect them as above mentioned ?
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Nikolay on August 23, 2018, 07:00:35 AM
This question seems to do that, in python:
https://stackoverflow.com/questions/43119001/how-to-add-a-link-to-a-visio-document-via-python

Example for the AutoConnect (easier to use than glueTo but less functional) is right in the documentation
https://docs.microsoft.com/en-us/office/vba/api/visio.shape.autoconnect

BTW, Visio has "layout shapes" command that provides some decent layout functionality. And the data visualizer, that can create diagrams from data using graph layout algorithm. It is not neato, but for common case - flowcharts or flowcharts with restrictions (lanes) - it is even better. Have you tried these?
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 23, 2018, 10:13:05 AM
Thanks a lot, that put me on the right direction
The VBA Visio Object model is here, well structured

https://docs.microsoft.com/en-us/office/vba/api/overview/visio

I guess the python class win32.com.client is just a proxy to expose the COM API to Python so I guess with thart module and the above page I have everything I need tostart playing with these
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Yacine on August 24, 2018, 10:49:34 AM
I had some time to play and used it for a very early concept of what a visio library for Jupyter could look like.

Most of you would ask "why?".
Because those notebooks offer the right balance between concatenating basic commands and complex scripts.
They are available and interactive.
Once the right sequence of commands is found, you can save and re-use it.
They offer an ecosystem of endless other applications: databases, statistics, file handling, AI, ...
The technical support is very good due to a huge community

Why not VBA?
- missing the "foreign" ecosystem
- less re-useable because the code is not so easily stored as separate file. It must be loaded in a stencil or the doc. --> less interactive and live improvment

Why not Visual Studio?
- Visual Studio is best for delivering closed solutions. "Living" projects almost impossible to hangle. Specially with a community.

Why not Linqpad?
- Very good, but:
  - proprietary, not open source
  - limited to Microsoft ecosystem.

----

So what did I think of in matter of functions?
1) Basic connection and file handling stuff
2) Functions to handle the basic pages, shapesheet, layers and formats stuff
3) Solutions for recurrent questions of the forum (display prop fields in shapes, smartshapes behaviours, etc.)


Let me know your comments, I look forward to a vibrant discussion.
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: vojo on August 24, 2018, 03:23:13 PM
played with same question

the problem with Python is its tailored to the Linux crowd (love to change their car's water pump daily)
- no real GUI (Tkinter, PYQT, etc all assume external graphics packages...so really cant bundle legally)
- Several GIT packages for Excell (work well)..but nothing really for Visio other than people playing
- Vpython more for scientific typs
- various drawing solutions in python immature or not maintained

I think a good path is the following
- effort to create a visio add on that can call python code (scripts or project) and can receive python calls.
    - hide all the minutia (similar to the various excell approaches)
- develop a family of python class instances for shapes in a module
    - fully articulated class instance a square and another for circle
    - add on can task via by class instance name
    - python can task by class instance name
    - for example
         Python side:
             square123 = square (color, size, <etc>)
             square456 = square (color, size, <etc>)
             def get_manifest:
                  <present list of shapes on python side...probably a list of tuples of (name, shape instance) >
             def get_shape (name, <etc>):
                  <present for example square123 and all its attributes to outside world>
             def set_shape (name, type, <etc>):
                  <set shape data or create a new shape>
             def del_shape (name, <etc>):
                  <deletes shape out of manifest...here line101112>

         VBA side:
             vbashape manifest = py.shape_manifest
             vbashape123 = py.get_shape (squar123, <etc>)
             vbashape456 = py.get_shape (squar456, <etc>)
             vbashape789 = py.get_shape (circle 123, <etc>)
             vbashape101112 = set_shape(line101112, <etc>)      //returns error since not in manifest

             size = vbashape 123.size
             vbashape 456.color = blue
             <update back to python...e.g manifest>
             vbashape 789.addprops (5 rows)
             vbashape789.props1 = author

The point is
- visio addon interfaces to python or maybe even the module directly
- python module is a library of shape definitions
- python program creates uniqes...5 squares, other non visio items, etc

Two way comms between Visio and the python program to modify shapes listed in the manifest from either side
(of course, the above has syntax problems but I think gives the general idea - both sides can create, delete, alter shapes)
(basically by refreshing the manifest then pick the shape...maybe VBA code has refresh manifest in the VBA shape binding)

Check out the python projects to deal with Excell...lots of good examples where...I know of 5 at least in pypi.
- Excell can task python (unique functions or even DB access )
- Python can task Excel (excell is the just the presentation screen)
- or both ways
             
                 
             
       
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: vojo on August 24, 2018, 03:24:47 PM
I guess I should have read the PDFs first ;-)
just make most of them callible from Visio side the world
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 25, 2018, 02:38:27 AM
This looks very interesting Yacine
I am not a programmer but a network engineer with some Python skills.
I will definitely help with testing and improvement ideas as I will use this on a daily basis

Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: PF4VisioGuy on August 25, 2018, 03:00:43 AM
One thing that I would like Visio to have is markdown language for text in text shapes
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Yacine on August 25, 2018, 01:52:13 PM
Quote from: vojo on August 24, 2018, 03:24:47 PM
just make most of them callible from Visio side the world
Jupyter to Viso is clear to me. The other way around not that much. Can you give some hints?
Title: Re: Do you forsee any problems with using Python to automate diagram creations ?
Post by: Benblack on November 25, 2022, 12:31:15 PM
I've been doing Python programming for about two years and I haven't had any problems that you're interested in right now. You can safely act and try something new, and professionals will always help you. This programming language is unique cause it is very flexible and suitable for different platforms, so there are almost never any problems with it use. If these problems arise, then they are either not so serious and they can be solved independently, or they can be solved with the help of the Internet. I very often serve on different forums and look for answers to various problems related to Python. Recently I saw your problem on https://soax.com/ (https://soax.com/) and in the same way I helped a person solve it, as I am helping you now. Good luck friend!