Search code examples
pythonwin32comvisio

python win32com - accessing Visio Custom Stencils


I have an example of how to add some Basic shapes to a Visio document while Visio is already open, but I am interested in accessing my own custom stencils which are in the More Shapes-> My Shapes tab on visio. I would have thought that I could just extend the below example to include the More Shapes path by replacing

stnObj = appVisio.Documents("Basic Shapes.vss")

with

stnObj = appVisio.Documents("More Shapes\My Shapes\mycustomStencil.vss")

but I keep getting an error at the line where I try to open the stencil because I'm getting the pathing wrong somehow.

Message=(-2147352567, 'Exception occurred.', (0, 'Drawing4 - Visio Professional', '\n\nThis file name is not valid.', None, 0, -2032466859), None)

This is the example I'm working off which seems to work just fine working with Basic Shapes. Note that Visio has to be open already when running the code. Anyone have an idea on how to programmatically access your own custom stencils with Python?

import win32com.client

appVisio = win32com.client.Dispatch("Visio.Application")
doc = appVisio.Documents.Add("Basic Diagram.vst")
pagObj = doc.Pages.Item(1)
stnObj = appVisio.Documents("Basic Shapes.vss")

shpObj1 = pagObj.Drop(stnObj.Masters("Square"), 4, 7)
shpObj1.Text = "Square text"

Solution

  • Sometimes ago i asked same question.
    We must use syntax like for convert stencil's path to raw string:

    appVisio.Documents.Open (r'C:\test\0001.vss')