【问题标题】:How to connect shapes in Visio using VBA & Python(pywin32)?如何使用 VBA 和 Python(pywin32)在 Visio 中连接形状?
【发布时间】:2021-08-04 05:17:04
【问题描述】:

我正在编写脚本以使用 Python 自动化 Visio 文档。我正在使用 win32com 模块,到目前为止,我做得很好。我学会了如何玩形状、大师和模板等;但是,我坚持连接形状。我搜索了许多论坛和讨论,但我自己无法得到任何明确的解释。

我决定创建一个 2 对象来从我添加的连接点连接它们。 在这里,我添加了大图,以便可以清楚地看到边缘的连接点。

然后,我尝试连接它们。我从文档中学到的是,首先我需要创建一个连接对象。然后我需要从这个 1D 对象创建 2 个单元格,它们是 begin("BeginX") 和 end("EndX") 点。然后,我需要将此端点粘合到对象上。

这是我的 Python 代码,取自 VBA 文档(我将其转换为 Python)。但是,“Geometry1.X1”等是什么,真的很难理解。

import win32com.client
app = win32com.client.Dispatch("Visio.Application")
app.Visible = True
doc = app.Documents.Open("d:\\X.vsd")
page = app.ActivePage

...

added_new_shape1 = page.Drop(master_shape_ex, 5, 5)
added_new_shape2 = page.Drop(master_shape_ex, 10, 5)

myConnector = page.Drop(app.ConnectorToolDataObject, 4, 10)
myConnectorBegin = myConnector.Cells("BeginX")
myConnectorEnd = myConnector.Cells("EndX")

vsoCellGlueToObject = added_new_shape1.Cells("Geometry1.X1")
vsoCellGlueToObject2 = added_new_shape2.Cells("Geometry1.X1")

myConnectorBegin.GlueTo(vsoCellGlueToObject)
myConnectorEnd.GlueTo(vsoCellGlueToObject2)

如果我运行我的代码,两个形状从角落连接起来。

然后我玩 X1,我看到连接端点正在改变,但我在这里很困惑。

很多问题浮现在脑海,但如果我有这两个的答案,我相信我会理解其中的逻辑。

  1. 如何通过指定形状上的点来连接形状?
  2. 如何使用连接点连接形状? (在这个例子中,我自己创建了形状并添加了连接点。使用脚本添加连接点是否重要?)

如果您能在这两个问题之外给我建议,我将非常高兴。

【问题讨论】:

    标签: python vba automation visio connector


    【解决方案1】:

    考虑这段代码:

    Python:

    import win32com.client
    
    app = win32com.client.Dispatch("Visio.Application")
    app.Visible = True
    #open Visio document and assign it to variable doc
    doc = app.Documents.Open(r"c:\Users\Alex20\Documents\Connect.vsdm")
    page = app.ActivePage
    
    # drop shape to the page from doc stencil by name "Master.4" at x,y coord.
    added_new_shape1 = page.Drop(doc.Masters("Master.4"), 2, 2)
    added_new_shape2 = page.Drop(doc.Masters("Master.4"), 4, 4)
    
    # create connection point for added_new_shape1
    # by adding row to 7 section (stores an object's connection points), after last exists row, unnamed rows
    conPt1 = added_new_shape1.AddRow(7, -2, 153) # .AddRow(visSectionConnectionPts, visRowLast, visTagCnnctPt)
    conRow1 = added_new_shape1.Section(7).Row(conPt1) #get the created row
    # set coordinates of the connection point (0) - x, (1) - y
    conRow1.Cell(0).FormulaU = "Width*1"
    conRow1.Cell(1).FormulaU = "Height*0.5"
    
    # create connection point for added_new_shape2
    conPt2 = added_new_shape2.AddRow(7, -2, 153)
    conRow1 = added_new_shape2.Section(7).Row(conPt2)
    conRow1.Cell(0).FormulaU = "Width*0.5"
    conRow1.Cell(1).FormulaU = "Height*1"
    
    # drop the connector onto page
    myConnector = page.Drop(app.ConnectorToolDataObject, 4, 10)
    myConnectorBegin = myConnector.Cells("BeginX") #get start point of the connector
    myConnectorEnd = myConnector.Cells("EndX") #get end point of the connector
    
    vsoCellGlueToObject = added_new_shape1.Cells("Connections.X1") # get early created connection point of the first shape
    vsoCellGlueToObject2 = added_new_shape2.Cells("Connections.X1") # get early created connection point of the second shape
    
    myConnectorBegin.GlueTo(vsoCellGlueToObject) # connect start point of the connector to shape's connection point
    myConnectorEnd.GlueTo(vsoCellGlueToObject2)
    

    相同(VBA):

    Sub Macro2()
        Set pg = Application.ActiveWindow.Page
        
        Set s1 = pg.Drop(ActiveDocument.Masters("Master.4"), 2, 2)
        Set s2 = pg.Drop(ActiveDocument.Masters("Master.4"), 4, 4)
    
        intRowIndex1 = s1.AddRow(visSectionConnectionPts, visRowLast, visTagCnnctPt)
        Set vsoRow1 = s1.Section(visSectionConnectionPts).Row(intRowIndex1)
        vsoRow1.Cell(visCnnctX).FormulaU = "Width*1"
        vsoRow1.Cell(visCnnctY).FormulaU = "Height*0.5"
    
        intRowIndex3 = s2.AddRow(visSectionConnectionPts, visRowLast, visTagCnnctPt)
        Set vsoRow2 = s2.Section(visSectionConnectionPts).Row(intRowIndex3)
        vsoRow2.Cell(visCnnctX).FormulaU = "Width*0.5"
        vsoRow2.Cell(visCnnctY).FormulaU = "Height*1"
    
        Set conn = pg.Drop(Application.ConnectorToolDataObject, 0#, 0#)
        
        Set vsoCell1 = conn.CellsU("BeginX")
        Set vsoCell2 = s1.Cells("Connections.X1")
        vsoCell1.GlueTo vsoCell2
        
        Set vsoCell1 = conn.CellsU("EndX")
        Set vsoCell2 = s2.Cells("Connections.X1")
        vsoCell1.GlueTo vsoCell2
    End Sub
    

    请参阅https://docs.microsoft.com/en-us/office/client-developer/visio/cells-visio-shapesheet-reference 上的单元格(Visio ShapeSheet 参考)

    【讨论】:

    • 感谢您的回复,但我仍然不知道为什么,但我很困惑。您能否在您写的行中添加更多解释?我试图通过修改您的代码来理解,但它给出了错误。
    • 就像为什么“visSectionConnectionPts”选择为7,如果我尝试写smt。不同,它给出了错误。
    • visSectionConnectionPts 是一个值为 7 的常量:docs.microsoft.com/en-us/office/vba/api/visio.vissectionindices
    • 您好,您能否在您的 python 代码中添加更多解释?
    • 尽我所能写:) 查看答案中的代码
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-18
    • 1970-01-01
    相关资源
    最近更新 更多