...not anything else!
Here's the python I am playing around with, a conglomeration of code from the deep reaches of the web.
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:\python\MyDrawing.vsdx")
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("Rectangle"), 6.5, 6.5)
added_new_shape2 = page.Drop(doc.Masters("Circle"), 3.25, 3.75)
added_new_shape3 = page.Drop(doc.Masters.ItemU("Diamond"), 5.25, 5.75)
# 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)
conRow2 = added_new_shape2.Section(7).Row(conPt2)
conRow2.Cell(0).FormulaU = "Width*0.5"
conRow2.Cell(1).FormulaU = "Height*1"
# create connection point for added_new_shape2
conPt3 = added_new_shape3.AddRow(7, -2, 153)
conRow3 = added_new_shape3.Section(7).Row(conPt3)
conRow3.Cell(0).FormulaU = "Width*0.5"
conRow3.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.X5") # get early created connection point of the second shape
#X1 = bottom (but top for shape2)
#X2 = right side? (but bottom for shape2)
#X3 = top? (but left side for shape2)
#X4 = left side? (but bottom for shape2)
#X5 = middle? (but right side for shape2)
#X6 = right side?
myConnectorBegin.GlueTo(vsoCellGlueToObject) # connect start point of the connector to shape's connection point
myConnectorEnd.GlueTo(vsoCellGlueToObject2)
# second connector onto page
myConnector2 = page.Drop(app.ConnectorToolDataObject, 4, 10)
myConnectorBegin2 = myConnector2.Cells("BeginX") #get start point of the connector
myConnectorEnd2 = myConnector2.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_shape3.Cells("Connections.X1") # get early created connection point of the second shape
myConnectorBegin2.GlueTo(vsoCellGlueToObject) # connect start point of the connector to shape's connection point
myConnectorEnd2.GlueTo(vsoCellGlueToObject2)
#Change text of shape
added_new_shape1.Text = "testing"
added_new_shape2.Text = "EMMA"
#Change color of shape
added_new_shape2.Cells("FillForegnd").FormulaForceU = "RGB(255,153,51)"
Now here's the error I'm getting:
C:\python>py createvisio2.py
Traceback (most recent call last):
File "C:\python\createvisio2.py", line 12, in <module>
added_new_shape3 = page.Drop(doc.Masters.ItemU("Diamond"), 5.25, 5.75)
File "<COMObject <unknown>>", line 2, in ItemU
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'MyDrawing.vsdx - Visio Professional', '\n\nObject name not found.', None, 0, -2032465660), None)
Why can I not create a Diamond by name? Oh, and I've tried it without ItemU as well, it doesn't work either way! I haven't found any shapes besides Rectangle and Circle that work to create via python... but when I record a macro, they all are referred to by shape name.
I appreciate any help!