问题描述
我正在编写脚本以使用 Python 自动化 Visio 文档。我正在使用 win32com 模块,到目前为止,我做得很好。我学会了如何使用形状、母版和模板等;然而,我坚持连接形状。我搜索了很多论坛和讨论,但我自己都没有得到任何明确的解释。
我决定创建一个 2 对象,将它们从我添加的连接点连接起来。 在这里,我添加了大图,以便可以清楚地看到边缘的连接点。
然后,我尝试连接它们。我从文档中了解到,首先我需要创建一个连接对象。然后我需要从这个一维对象创建 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)
如果我运行我的代码,2 个形状从角连接起来。
然后我玩 X1,我看到连接端点正在改变,但我在这里很困惑。
想到了很多问题,但如果我有这两个的答案,我相信我会理解其中的逻辑。
如果您能在这两个问题之外给我一些建议,我会很高兴。
解决方法
考虑这个代码:
蟒蛇:
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,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,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)
Set s2 = pg.Drop(ActiveDocument.Masters("Master.4"),4)
intRowIndex1 = s1.AddRow(visSectionConnectionPts,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,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 参考)