How to change Visio font name and color using Pyth

2020-03-19 03:52发布

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

2条回答
成全新的幸福
2楼-- · 2020-03-19 04:06

This will set the color and font.

# Microsoft Office Visio Constants
visCharacterFont = 0
visCharacterColor = 1
visSectionCharacter = 3
visCharacterDblUnderline = 8
visSectionFirstComponent = 10

Set the text color

vsoShape.CellsSRC(visSectionCharacter, 0, visCharacterColor).FormulaU = "THEMEGUARD(RGB(0,0,0))"

To set the font

vsoShape.CellsSRC(visSectionCharacter, 0, visCharacterFont).FormulaU = 100

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
3楼-- · 2020-03-19 04:07

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 )
查看更多
登录 后发表回答