I have a Visio template which has its own stencil(library of shapes). Imagine, I have stencil which has more shapes; therefore, I want to delete the stencil of the template Visio, and want to add mine. I searched this a lot on the internet but couldn't find a solution. I can simply add my shapes to template document stencil, however, I want to do this using Python since I want to automate things, and in every template I don't want to do this.
In office VBA page, I found this but couldn't implement to my script. (Adding a document object based on both template and stencil.)
Here is the link: https://learn.microsoft.com/en-us/office/vba/api/visio.documents.add
Public Sub AddDocument_Example()
Dim vsoDocument As Visio.Document
'Add a Document object based on the Basic Diagram template.
Set vsoDocument = Documents.Add("Basic Diagram.vst")
'Add a Document object based on a drawing (creates a copy of the drawing).
Set vsoDocument = Documents.Add("Myfile.vsd ")
'Add a Document object based on a stencil (creates a copy of the stencil).
Set vsoDocument = Documents.Add("Basic Shapes.vss")
'Add a Document object based on no template.
Set vsoDocument = Documents.Add("")
End Sub
I don't know maybe deleting current stencil might be a problematic because template Visio has already shapes from that stencil.
I'm open to new ideas or solutions. If you help me, I would be very appreciated.
My current code:
import win32com.client
app = win32com.client.Dispatch("Visio.Application")
app.Visible = True
doc = app.Documents.Open("d:\\X.vsd") #Open template document
custom_stencil = app.Documents.Add("d:\\custom_stencil.vssx") #Trying to add custom stencil
page = app.ActivePage
#Show the all items in stencil
for shape in doc.Masters:
print(shape)
Each document has its own masters. When you just open a stencil document, it does not bring the masters from that stencil document into your template document, it just opens that stencil document (or, more precisely, a copy of that stencil, in your code). If you did something like this loop, probably you'd see the masters:
Please note that python is not a common choice when doing Office (Visio) automation. You usually do that with VBA. This may be the reason you don't find that many samples around.
Here I posted an example of creating a shape using python a few years ago: Use .vss stencil file to generate shapes by python code (use .vdx?)