Visio Guy

Visio Discussions => Programming & Code => Topic started by: Surrogate on August 16, 2022, 11:27:08 AM

Title: Python: Cant open exist document
Post by: Surrogate on August 16, 2022, 11:27:08 AM
Today, I try open Visio document with this simple code
import win32com.client as w32
application = w32.Dispatch("Visio.Application")
application.Visible = True
application.AlertResponse = 0
# application.Documents.Open ('C:\Program Files\Microsoft Office\root\Office16\visio content\1033\dtlnet_m.vstx')
application.Documents.Open ('C:\test\0001.vsd')
Code works fine with build-in templates like 'dtlnet_m.vstx', but cant open any vsd(*)-document at my PC !!!
I try use methods: Open, OpenEx, Add.
Title: Re: Python: Cant open exist document
Post by: Paul Herber on August 16, 2022, 11:34:37 AM
Double-quotes, not single-quotes.


application.Documents.Open ("C:\test\0001.vsd")
Title: Re: Python: Cant open exist document
Post by: Surrogate on August 16, 2022, 11:38:47 AM
Quote from: Paul Herber on August 16, 2022, 11:34:37 AM
Double-quotes, not single-quotes.
I try both !
When I copy file path from Windows Explorer, first letter is small.
Title: Re: Python: Cant open exist document
Post by: Surrogate on August 16, 2022, 12:45:48 PM
Met this issue in Jupyther Notebook. Try also Jupyther Lab, VS Code.
Title: Re: Python: Cant open exist document
Post by: Yacine on August 17, 2022, 07:23:36 AM
About the quotes:
Python accepts single, double and triple quotes equally. The triple ones are for multi line string values (long text).

The important thing is the back slash, it is an escape character.
eg:
  \n ist not "\n" but a line return (vbCrLf)
  \t is a tab character (chr(9) from memory)

There are different ways to let Python interpret them correctly.
  replace them by double back slashes --> application.Documents.Open ('C:\\test\\0001.vsd')
  or declare the string as "raw" so as not to interpret any escape character. Write lower "r" in front of the string. --> application.Documents.Open (r'C:\test\0001.vsd') - preferred method since much simpler.
 
Title: Re: Python: Cant open exist document
Post by: Surrogate on August 17, 2022, 07:55:51 AM
Quote from: Yacine on August 17, 2022, 07:23:36 AMor declare the string as "raw" so as not to interpret any escape character. Write lower "r" in front of the string.
Excellent! Thank you!!!
Quote from: Yacine on August 17, 2022, 07:23:36 AM
replace them by double back slashes --> application.Documents.Open ('C:\\test\\0001.vsd')
I try this trick yesterday with no luck, but today ot works  :o
Title: Re: Python: Cant open exist document
Post by: Yacine on August 17, 2022, 08:13:37 AM
Quote from: Surrogate on August 17, 2022, 07:55:51 AM
I try this trick yesterday with no luck, but today ot works  :o

It works only on odd days. :D :D :D
Title: Re: Python: Cant open exist document
Post by: wapperdude on August 17, 2022, 01:12:02 PM
Snake bit.