Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change Visio font name and color using Python?

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
like image 682
John Bessire Avatar asked Sep 02 '15 00:09

John Bessire


People also ask

How do I change font color in Visio?

Right-click the shape, point to Format, and select Text. In the Text dialog box, on the Text Block tab, under Text background, select Solid color. In the list next to Solid color, select the color that you want. Click Apply and then click OK.

How do I change the font in Visio?

Click the Text tab and, in the Font list, click the font that you want to use for all shapes in the drawing. Click the Line, Fill, Shadow, and Connector tabs to adjust those options if you want to. Click OK.

How do I change the default font in Visio 2010?

The default font option is under "Effects". Create a custom Theme Effects and set the default font there. Create a custom theme and then apply it to your Visio file. You do that via the Design tab.


1 Answers

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.

import win32com.client
from win32com.client import constants as vis

# gencache.EnsureDispatch will ensure constants are built
app = win32com.client.gencache.EnsureDispatch( 'Visio.Application' )

# hide the window if you want
#app.Visible = 0

shape = app.ActivePage.DrawRectangle(1,1,2,2)

# text in shape
shape.Text = 'Text'

# fill color of shape
shape.Cells( 'Fillforegnd' ).FormulaU = 'RGB(255,255,0)'

# shape without fill
shape.FillStyle = "None"

# color of border line
shape.Cells( 'LineColor' ).FormulaU = 'RGB(0,0,255)'

# shape without border line
shape.LineStyle = "None"

# line pattern, numbers for patterns can be looked up in visio, they are displayed in the pattern drop down
shape.Cells( 'LinePattern' ).FormulaU = '3'

# line weight
shape.Cells( 'LineWeight' ).FormulaU = '0.1'

# text color
shape.CellsSRC( vis.visSectionCharacter, 0, vis.visCharacterColor ).FormulaU = 'RGB(255,0,0)'

# size of text
shape.Cells( 'Char.size' ).FormulaU = '20 pt'

# vertical alignment of text, values are 0,1,2
shape.Cells( 'VerticalAlign' ).FormulaU = '1'

chars = shape.Characters

# here you can set which characters the following styles will be applied to
chars.Begin = 0
chars.End = chars.CharCount

# text bold, italic and underline styles, add to combine
chars.CharProps( vis.visCharacterStyle, vis.visBold + vis.visItalic + vis.visUnderLine )

# text strikethrough
chars.CharProps( vis.visCharacterStrikethru, True )
like image 142
daign Avatar answered Sep 19 '22 12:09

daign