0

我有一个 Visio 模板,它有自己的模板(形状库)。想象一下,我有更多形状的模板;因此,我想删除模板 Visio 的模板,并想添加我的模板。我在互联网上搜索了很多,但找不到解决方案。我可以简单地将我的形状添加到模板文档模具中,但是,我想使用 Python 来执行此操作,因为我想使事情自动化,并且在每个模板中我都不想这样做。

在 office VBA 页面中,我发现了这一点,但无法在我的脚本中实现。(添加基于模板和模板的文档对象。)

这是链接:https ://docs.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

我不知道删除当前模具可能会出现问题,因为模板 Visio 已经具有该模具的形状。

我乐于接受新的想法或解决方案。如果你帮助我,我将不胜感激。

我当前的代码:

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

1 回答 1

1

每个文档都有自己的主人。当您只打开一个模板文档时,它不会将该模板文档中的母版带入您的模板文档,它只是打开该模板文档(或者更准确地说,在您的代码中打开该模板的副本)。如果你做了类似这样的循环,你可能会看到大师:

for shape in custom_stencil.Masters:
    print(shape)

请注意,在执行 Office (Visio) 自动化时,python 不是常见的选择。您通常使用 VBA 来执行此操作。这可能是您找不到那么多样本的原因。

这里我贴了一个几年前使用python创建形状的例子: Use .vss stencil file to generate shapes by python code (use .vdx?)

于 2021-05-13T20:04:32.550 回答