I am using Python 2.7 with the win32com.client and trying to figure out how to change the font name and color for a Microsoft Visio 2013 shape.
The code below creates a rectangle shape on a Visio document that is already open. This code is working and sets the shape color, text and line width without any problems.
import sys, win32com.client
visio = win32com.client.Dispatch("Visio.Application")
vsoShape1 = visio.ActivePage.DrawRectangle(1,1,2,2)
vsoShape1.Cells("LineColor").FormulaU = 0
vsoShape1.Cells("LineWeight").FormulaU = "2.0 pt"
vsoShape1.FillStyle = "None"
vsoShape1.Text = "This is a test"
vsoShape1.Cells("Char.size").FormulaU = "20 pt"
Different methods were tried to change the font name and font color which resulted in error messages.
Theses two lines of code both result in this error message: pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Drawing4 - Visio Standard', u'\n\nUnexpected end of file.', None, 0, -2032466967), None)
vsoShape1.Cells("Font.Name").FormulaU = "Courier"
vsoShape1.Cells("Font.Bold").FormulaU = "True"
The next three lines of code all resulted in a similar error message without the end of file error: pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Drawing4 - Visio Standard', u'\n\nNAME?', None, 0, -2032466907), None)
vsoShape1.Cells("Char.Font").FormulaU = "Courier"
vsoShape1.Cells("Char.colorIndex").FormulaU = 16
vsoShape1.Cells("Font.Bold").FormulaU = 0
A few more attempts resulted in: DrawRectangle.xxxxx can not be set.
vsoShape1.fontName = "Courier"
vsoShape1.Bold = True
vsoShape1.Bold = 1
This will set the color and font.
Set the text color
To set the font
The number for the font is described as "An integer that represents an index into the Fonts collection installed on a system. Zero (0) represents the default font". The documentation doesn't say whether this integer is always the same or varies depending on the fonts that are installed. I got the number by running a macro and looking at the output of the VB script.
I see you tried different things to make the text bold without success. I found a solution to this and I'm going to post it along with other styling options. It was annoying to figure them all out because there is hardly any clear documentation, so I hope this will help someone.