0

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)
bailofwZ
  • 123
  • 1
  • 10

1 Answers1

1

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:

for shape in custom_stencil.Masters:
    print(shape)

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?)

Nikolay
  • 10,752
  • 2
  • 23
  • 51
  • Thanks for reply @Nikolay Since I've been learning Python for 6 months, it's the only option for me now :( So, what I understand is, I should open stencil document manually each time and use its shapes in the current document. Do you foresee any problem, if I do this in every program? Moreover, I should open this stencil template using OpenEx right? – bailofwZ May 13 '21 at 20:20
  • There is no difference how you open it. You can also copy your masters to your template document ahead (manually for example), then you won't need the stencil document at all. You can see the masters that exist in the document in the "Document Stencil" window, which you can turn on in the "Developer" tab (checkbox). VBA is really simple, it was designed to be simple. You probably can do it in just a few days. – Nikolay May 13 '21 at 21:08
  • FYI: Visio Stencils, Masters and Shapes: How are they related? https://www.experts-exchange.com/articles/2680/Visio-Stencils-Masters-and-Shapes-How-are-they-related.html (it also explains what "document stencil" is) – Nikolay May 13 '21 at 21:14
  • Thanks for your clear explanation. I totally understand the idea of master, shape and so on. I think I think I will continue using Python until I stuck very hard :) I also searched learning VBA process, it seems really easy as you suggested. I have one more question for you, I tried OpenEx and add constants which are "visOpenRO" and "visOpenDocked"; however, I couldn't understand their affect, I delete these constants and rerun, but it seems no difference :( To check whether my constants work or not, I tried "visOpenHidden", it works perfectly. So, "visOpenRO" and "visOpenDocked ? – bailofwZ May 13 '21 at 21:43
  • By the way, I tried to comment your post to ask my question that I wrote above but I don't have enough reps to comment, website doesn't allow me since I'm newbie :( https://stackoverflow.com/questions/33488183/use-vss-stencil-file-to-generate-shapes-by-python-code-use-vdx – bailofwZ May 13 '21 at 21:49
  • @Talat you might find these videos useful in understanding how Visio works. THey don't deal with Python specifically but object model and approach is more or the same irrespective of the language https://visualsignals.typepad.co.uk/vislog/2016/04/new-visio-training-videos.html – JohnGoldsmith May 14 '21 at 07:54