Automatic creating a gradient color shape in Visio using Python

90 views Asked by At

I'm trying to automate figure/shape generation in MS Visio via some python scripting using win32com. More specifically I'm having issues make converting/creating a shape with gradient fill.

I have a fairly simple python script where I only create a shape and then enable gradient to keep it simple. Enabling gradient, setting direction/angle works without issue. When I try to specify color and position/stops of the gradient it simply fails. I tried with a large varieties of the stop formular including newline, using only one row, and took a deep dive into the shapesheet in Visio and Microsoft documenttaion to see if there are any points but without luck. I haven't been able to find any documentation about how to set the formula via Python, or alternative ways to manipulate those field/cells specifically.

I have the following codesnippet - The FillGradient part is inspired by ChatGBT since I had a hard time finding any relevant documentation:

app = win32.Dispatch('Visio.Application')
app.Visible = True
page = self.app.ActivePage
shape_name = "Process"
mastObj = self.stnObj.Masters(shape_name)
shape = page.Drop(mastObj, xPos, yPos)
# The following yields same results:
shape = page.DrawRectangle(1.0, 1.0, 2.0, 2.0)
shape.Text = "My shape name"
shape.Cells('FillForegnd').FormulaU = "RGB(255,0,0)"
gradient_type = 1  # Linear gradient
gradient_angle = 45  # Angle in degrees (0 for horizontal)
Set gradient fill properties
shape.Cells("FillGradientEnabled").FormulaU = "TRUE"
shape.Cells("FillGradientDir").FormulaU = str(gradient_angle)
shape.Cells("FillGradientAngle").FormulaU = str(gradient_angle)
stops_formula = "RGB(255,0,0),0%,0:RGB(0,0,255),100%,1"
shape.Cells("FillGradientStops").FormulaU = stops_formula

I simply get the following error

shape.Cells("FillGradientStops").FormulaU = stops_formula
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "<COMObject Drop>", line 2, in Cells
pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, 'Visio Professional', '\n\nUnexpected end of file.', None, 0, -2032466967), None)

So, I'm looking for either a way to construct a correct formular or a way to manipulate the cells/rows of Fill Gradient Stops with the same information.

1

There are 1 answers

1
Surrogate On BEST ANSWER

I did not use fill gradients in my drawings…
I just add rectangle and look what is in its Gradient Properties section enter image description here
I cant find FillGradientStops cell there! Also I cant find this cell in Shapesheet's MS online documentation

IMHO you try change value in cell which not exist!.

UPDATE I found this article by John Goldsmith.
We have Fill Gradients section
Fill Gradients Stop section
Where each row contains 3 cells: Color, ColorTrans and Position.
You can try use syntax like for first line:

shape.Cells("FillGradientStops.GradientStopColor").Formula = "1"
shape.Cells("FillGradientStops.GradientStopColorTrans").Formula = "7%"
shape.Cells("FillGradientStops.GradientStopPosition").Formula = "40%"

For second and other rows you must and rows index like

shape.Cells("FillGradientStops.GradientStopColor[i]").Formula = "2"
shape.Cells("FillGradientStops.GradientStopColorTrans[i]").Formula = "10%"
shape.Cells("FillGradientStops.GradientStopPosition[i]").Formula = "60%"

But best way is use CellsSRC syntax!