Visio Guy

Visio Discussions => Programming & Code => Topic started by: Paul Herber on January 30, 2022, 03:03:11 PM

Title: Visio and Python
Post by: Paul Herber on January 30, 2022, 03:03:11 PM
I've been meaning to have a look at Python for a while, finally got around to it, and I've documented it as well for anyone else on this course ...

https://www.paulherber.co.uk/stuff-about-visio/visio-and-python/ (https://www.paulherber.co.uk/stuff-about-visio/visio-and-python/)
Title: Re: Visio and Python
Post by: wapperdude on January 30, 2022, 03:16:15 PM
I hate snakes.  Ask Yacine.  He's been encouraging me to join Slytherin House for years.
Title: Re: Visio and Python
Post by: Paul Herber on January 31, 2022, 05:49:48 PM
Quote from: wapperdude on January 30, 2022, 03:16:15 PM
Slytherin House

Ah, an HP reference.
No Monty allowed in HP. Lot's of Monty in Python!

Title: Re: Visio and Python
Post by: Paul Herber on February 01, 2022, 07:38:14 PM
Any suggestions for an app that I could host on my website where we could create an archive of Visio Python code snippets? Open-source would be first-rate.
Title: Re: Visio and Python
Post by: vojo on February 02, 2022, 02:24:31 AM
you mean something like this for visio python
http://visio.mvps.org/VBA/default.html
Title: Re: Visio and Python
Post by: Paul Herber on February 02, 2022, 09:10:10 PM
Quote from: vojo on February 02, 2022, 02:24:31 AM
you mean something like this for visio python
http://visio.mvps.org/VBA/default.html

Something along those lines perhaps, if not a web page with code then maybe some form of collaboration system where registered users can donate code.
Title: Re: Visio and Python
Post by: Paul Herber on February 03, 2022, 11:07:16 AM
How about a wiki? I could install DocuWiki quite easily.
Title: Re: Visio and Python
Post by: Paul Herber on February 03, 2022, 06:09:22 PM
I've created
https://python-and-visio.paulherber.co.uk/ (https://python-and-visio.paulherber.co.uk/)
Nothing of interest there yet ... It'll be public readable but writeable by invitation only.
If you wish to contribute please email me.
Title: Re: Visio and Python
Post by: vojo on February 04, 2022, 03:30:34 AM
good start!!!!  Thx
Title: Re: Visio and Python
Post by: Yacine on February 05, 2022, 12:21:08 PM
Hello Paul,
I spent a couple of hours on the wiki and came to the following conclusion.

The Visio-Python couple is a much over-estimated problem, or hurdle. What ever you may name it.

There are only two things needed:
- a propper Python installation and
- a handle on Visio's objects.

That is all that is needed and it resolves all questions and fears.

Really! That's the end of the story.

Well not really, since the fun starts with leveraging Python's power to work with Visio.

That brings us to your idea about code snippets. But they won't particularly be some Visio-Python things, but rather Other-App-Python with some focus on Visio ones.
I'm struggling finding the right wording.

Basically, after having found how to interact with Visio, it is all but a matter of pure Python.
Special would be the fact that we deal with a COM-capable program,
and further special that we deal with a graphics program and a bit more with Visio.
Arrgh! It's not getting any better. Sorry!

Anyway, having mastered the "art of accessing Visio objects", all further articles can only be Examples on how to use it.

I've added an article about pure python stuff and one for examples one might want to see.

As for your wiki,
You see that I am using the forum to comment on it. I somehow miss a chat fuction in the wiki itself. You may argument that Visguy's forum is good enough!?!
I would also wish to have more colaboration from other visioneers. An open (or more open) wiki might me a solution? Specially for the wish list.
I'm not so fund of your wiki's possibilities.

So far for a lazy Saturday morning ...
yours,
Y.
Title: Re: Visio and Python
Post by: Yacine on February 05, 2022, 12:37:29 PM
I would suggest to loosen the registration process to the wiki in order to get more users involved.
The essence of a wiki being, that every edit can be corrected afterwards.
I plead for an optimistic approach, waiting for bad things to happen.
Title: Re: Visio and Python
Post by: Paul Herber on February 05, 2022, 03:19:40 PM
Hi Yacine, wow, you've expanded the wiki a lot. You seem to be an experienced programmer, so to you it seems like an easy task to interface Python to Visio. To me Python is a new concept, I spent my working life using assembler, C and that ilk. Looking at the type of questions asked in this forum about VBA programming, and the sort of VBA listings like:
http://visio.mvps.org/VBA/default.html
I see this wiki as a repository for getting people up to speed and some basic on how to loop through pages, shapes, access layers and change shape attributes.
Unless Chris wants to add a Python category to this forum...
I have invited a couple of others, but I don't want to open it up to the public for write access yet, keeping spammers at bay can be a nightmare.
Anyone interested in Python on this forum must have seen the invitation above, and they can see it again, now, please let me know your interest!
Title: Re: Visio and Python
Post by: Surrogate on February 05, 2022, 04:40:22 PM
Quote from: Yacine on February 05, 2022, 12:21:08 PM
There are only two things needed:
- a propper Python installation and
- a handle on Visio's objects.
A stumbling block for me: installing Python

Half a year ago I installed Python 3.8 and started to learn the basic operations. When I wanted to work with the openpyxl library there appeared to be a problem with the path where python was installed. There seems to be a limit of the path length (80 characters) and also my path contained cyrillic characters.  :o
Yesterday I uninstalled and installed Python 3.10, in the c:\python folder and again some problems. An error in the import win32com.client line pops up. PATH problem again  (https://visio.getbb.ru/images/ranks/visio_getbb_ru/huh.gif)
Title: Re: Visio and Python
Post by: Paul Herber on February 05, 2022, 05:19:43 PM
I think this one of the doubts I have about Python, it can be fine for creating and using scripts for your own use, but when you want to distribute them to the public or customers then you have no control over which packages the user has installed. Programming in C# doesn't have that problem but I don't know about VBA, probably not.
Title: Re: Visio and Python
Post by: Yacine on February 06, 2022, 11:07:33 AM
Quote from: Paul Herber on February 05, 2022, 03:19:40 PM
To me Python is a new concept, I spent my working life using assembler, C and that ilk.
Assembler and C? That is what I call "hardcore" programming.
Python is kindergarten compared to them.

It has some hurdles, I agree.
And you two nailed them, PATHs and the distribution of applications.
That said, they are only hurdles not limitations. Both can be solve
I will try to come back on them in the wiki.


Meanwhile remember Stackoverflow (https://stackoverflow.com/) is Python's Google.

Quote from: Paul Herber on February 05, 2022, 03:19:40 PM
I see this wiki as a repository for getting people up to speed and some basic on how to loop through pages, shapes, access layers and change shape attributes.

I've set up some examples and hope having caught the essence of your question.
You will notice that the "visio" part of the code is really tiny. Most of the code is basic Python stuff.
Let me know your opinion.
https://python-and-visio.paulherber.co.uk/doku.php?id=examples_of_python_programs (https://python-and-visio.paulherber.co.uk/doku.php?id=examples_of_python_programs)


Quote from: Paul Herber on February 05, 2022, 03:19:40 PM
Unless Chris wants to add a Python category to this forum...
This would be great.
Where is Chris anyway? Haven't read anything from him in months! Chris, are you fine?

Quote from: Paul Herber on February 05, 2022, 03:19:40 PM
I have invited a couple of others, but I don't want to open it up to the public for write access yet, keeping spammers at bay can be a nightmare.
Anyone interested in Python on this forum must have seen the invitation above, and they can see it again, now, please let me know your interest!
Sure!
Title: Re: Visio and Python
Post by: Paul Herber on February 06, 2022, 11:33:08 AM
I think you need to put your name on this editing somewhere, you need to take the credit for it.
Title: Re: Visio and Python
Post by: Yacine on February 06, 2022, 11:35:53 AM
I'm fighting for the cause. ;) :D
Title: Re: Visio and Python
Post by: Yacine on February 06, 2022, 02:11:19 PM
Quote from: Surrogate on February 05, 2022, 04:40:22 PM
Quote from: Yacine on February 05, 2022, 12:21:08 PM
There are only two things needed:
- a propper Python installation and
- a handle on Visio's objects.
A stumbling block for me: installing Python

Half a year ago I installed Python 3.8 and started to learn the basic operations. When I wanted to work with the openpyxl library there appeared to be a problem with the path where python was installed. There seems to be a limit of the path length (80 characters) and also my path contained cyrillic characters.  :o
Yesterday I uninstalled and installed Python 3.10, in the c:\python folder and again some problems. An error in the import win32com.client line pops up. PATH problem again  (https://visio.getbb.ru/images/ranks/visio_getbb_ru/huh.gif)


I work on my employer's computer which has Visio installed and - luckily - allowed me to install Miniconda (over a year ago)
and a second, personal one - which has Linux installed, because I am too greedy to pay for Windows and Visio. But it has Miniconda installed.

Bad constellation for investigating on path problems. I can't afford to mess up my "company" computer and can't really try windows and visio stuff on my personal one.




Anyway, let's dive into that ugly problem!
As matter of fact, when I tried checking my wiki examples in vs code, I also got path problems. Spent some time on it and gave up! Shame!
But giving up is not acceptable! (The world beyond this door is so green and shiny ;) ).
The target being to have ONE working, dumb and easy solution for ALL "visioneers"!


Lazy solution:
Use the installers of the heavier installations (Anaconda, Miniconda, ...) to set up the paths.


Hard-working solution:
Try different settings in editing window's path setting.
https://www.opentechguides.com/how-to/article/windows-10/113/windows-10-set-path.html (https://www.opentechguides.com/how-to/article/windows-10/113/windows-10-set-path.html)


I would really appreciate your feedback,
Y.


PS: By the way, there are tons of tutorials showing this process in Linux. But since "Visio" is "Windows", our solution also needs to be a "Windows" one!
Title: Re: Visio and Python
Post by: Surrogate on February 07, 2022, 10:01:14 AM
Quote from: Yacine on February 06, 2022, 02:11:19 PMLazy solution:
Use the installers of the heavier installations (Anaconda, Miniconda, ...) to set up the paths.
I already had the previously downloaded Miniconda, installed today but it did not work without intervention  :o
Then I installed Anaconda, a lot of packages installed at once. everything worked almost immediately.
Powershell default command conda activate base, replaced by .\conda activate base.


Hallelujah !!!
Title: Re: Visio and Python
Post by: Yacine on February 07, 2022, 10:44:09 AM
If now you can also get Jupyter lab running, then we can start playing. :D :D :D


BTW, don't forget to add a new environment, that you can mess up.
Title: Re: Visio and Python
Post by: wapperdude on February 10, 2022, 05:16:33 PM
@Yacine:  I bet I'm lazier than you!!! ...reference your comment in another post.  I'm to lazy to fetch that linik!   ;) ;D

I've been following the serpentine comments in this post, and when reference to thim in other posts occurs. 

My take-away, from the perspective of the "average" user / newbie would be "Really!?!  Why???"  The appeal seems like it would be of more interest for a serious, full-time develper.  VBA is built-in.  Nothing extra to install.  Not terribly difficult to learn ... if you devote reasonable amount of time.  AFAIK, all coding paths need investment time.  None suitable for the spoonfed, easy button, I want a solution now crowd.  But, VBA, because of its age, has a bounty of existing code macros that come close to plug n play.  Add to that a host of websites that have forums to address solutions.

Then, there's me.  Fully retired.  No driving need for Visio, other than participation in the Forum, and  perhaps, occassional DIY projects.  None of which warrant the need to delve into Python and the numerous enabling/support apps needed to go with it.  Indeed, most of my Visio needs don't even involve coding.  So, I watch all of this out of fascination.   

The Forum regulars need to forge a path and must demonstrate Python's utility and benefit,,,with actual examples/application, to convince Chris that there's merit and interest, that it warrants a separate, dedicated section on the Forum.
Title: Re: Visio and Python
Post by: vojo on February 10, 2022, 09:38:35 PM


https://software-solutions-online.com/vba-vs-python/#:~:text=Python%20is%20a%20General%2DPurpose,the%20Visual%20Basic%20Runtime%20Library. (https://software-solutions-online.com/vba-vs-python/#:~:text=Python%20is%20a%20General%2DPurpose,the%20Visual%20Basic%20Runtime%20Library.)

https://dataresident.com/python-vs-vba/ (https://dataresident.com/python-vs-vba/)

It looks like the big difference is VBA is tied to MS platforms (and since Visio is likewise tied to MS platforms, might be moot)
Other than that, its a fashion statement:  I am sure there is somebody out there doing Pascal to Visio but not many do anything with Pascal anymore. ;-)

Psersonally I have done alot with both
- Upside VBA can do OOP and does interface seemless to MS tools
- downside of VBA is the need to defeat all MS security issues
- Upside of Python, alot and I mean alot of free modules for very complex tasks  (Try numpy or scipy in VBA).
- downside of Python is that editor uses alot of spacing to establish context....hard to debug but getting better
-
Title: Re: Visio and Python
Post by: wapperdude on February 10, 2022, 11:16:58 PM
Good links Vojo!
Title: Re: Visio and Python
Post by: Visisthebest on February 11, 2022, 05:10:40 AM
Actually I see you can program Python by creating Visio diagrams:

https://github.com/vesellov/visio2python

Does that mean we can program Visio by programming Python with Visio diagrams? [mind=blown!]  ;D ;D ;D

If we create self-improving Visio diagrams that write Python code that creates better Visio diagrams have we achieved the Visio singularity?

(All kidding aside, I think Python is an excellent programming language)

And this tool:

https://github.com/vesellov/visio2python

is another amazing demonstration of what is possible with Visio!
Title: Re: Visio and Python
Post by: Yacine on February 11, 2022, 05:40:38 AM
@Wapperdude,
You are absolutely right with your statement, that Python is not suited for newbies and people looking for a quick way to draw, but so are - at a lower level - Visio's pro features.
At work, none of my colleagues would dare opening a shapesheet or write VBA code (for Visio at least - Excel is another story).
It took me probably half a year to grasp the concept of shapesheets.

So we are an elitary club of people trying to stretch the boundaries of Visio. Yesterday we've even implemented that crazy for loop (http://visguy.com/vgforum/index.php?topic=9786.msg44422;topicseen#msg44422) in the shapesheet - something M$ did not come up with.
We've mastered the shapesheet, we know a lot about VBA and here is a new tool that can do even more stuff, so why not try it?

This complicated installation is definitely something that shall become easier, but otherwise the technology is already quite mature. As a matter of fact, I use it on a daily basis for automating dumb tasks.

By no means would Python replace VBA. It is a complement.

VBA has also a lot of advantages. Forms are easier to write and handle, it is built in, better debugging, code completion (in a way), etc.


The day Wapperdude comes to us with some Python code, I'll throw a party.  ;D :o 8)
Title: Re: Visio and Python
Post by: wapperdude on February 11, 2022, 06:17:19 AM
Hope you invite me to the party!!!

Yeah.  At work, I was the "go to guy" for all things Visio. I remember, "Shapesheet???  We're allowed to modify the shapesheet!?!  Really?!?
Title: Re: Visio and Python
Post by: Yacine on February 11, 2022, 06:43:16 AM
Quote from: wapperdude on February 11, 2022, 06:17:19 AM
Hope you invite me to the party!!!
Sure I will. You'll be the honor guest.
Title: Re: Visio and Python
Post by: Yacine on February 11, 2022, 08:39:40 AM
I would like to repeat my offer to set up a teams meeting.
Rather during the weekend (Saturday?).
Time? Paul is in my time zone. Surrogate is some hours ahead. If the US wanted to join, they are some 7 hours back.
Saturday 7 p.m. German time (UK = 6 pm, Russia = 9pm - 12pm, US = 10am - 1pm)
Let me know.


I've also seen these Youtube live streams, where people can comment. Dunno what is necessary to implement such a thing.
Title: Re: Visio and Python
Post by: Surrogate on February 11, 2022, 09:48:16 AM
Sorry, but i cant take part in your meeting  :(

Today i left Moscow, i'll go to Russian country-side. Not sure that i find there good internet connection...

I''ll be back in Monday, may be in Tuesday.
Title: Re: Visio and Python
Post by: wapperdude on February 11, 2022, 03:01:05 PM
As FYI, Saturday doesn't work...Grandson's birthday.  But, my participation would be more of onlooker anyway.