Cannot open Visio document with Python

女生的网名这么多〃 提交于 2019-12-11 02:34:39

问题


I am attempting to do some automation in Visio using Python. I am able to open the Visio application and create a new document, but cannot open an existing document. Here is the code that I am trying.

  import win32com.client

  visio = win32com.client.Dispatch("Visio.Application")  # this works
  doc = visio.Documents.Open("C:\Users\username\test.vsd") # nope

The error I get back is

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "<COMObject <unknown>>", line 3, in OpenEx
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Microsoft Visio', u'\n\nFile not found.', None, 0, -2032465466), None)

I have tried using visio.Documents.OpenEx as well and get the same answer. Any thoughts?


回答1:


I am guessing that this might work -

doc = visio.Documents.Open("C:\\Users\\username\\test.vsd")



回答2:


Instead of forward slashes you can use backward ones

doc = visio.Documents.Open(r"C:\Users\username\test.vsd")

or

doc = visio.Documents.Open("C:/Users/username/test.vsd")


来源:https://stackoverflow.com/questions/26001984/cannot-open-visio-document-with-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!