Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用Python更改Visio字体名称和颜色?_Python_Visio_Win32com - Fatal编程技术网

如何使用Python更改Visio字体名称和颜色?

如何使用Python更改Visio字体名称和颜色?,python,visio,win32com,Python,Visio,Win32com,我正在win32com.client上使用Python 2.7,并试图找出如何更改Microsoft Visio 2013形状的字体名称和颜色 下面的代码在已打开的Visio文档上创建一个矩形。这段代码正在运行,设置形状颜色、文本和线宽没有任何问题 import sys, win32com.client visio = win32com.client.Dispatch("Visio.Application") vsoShape1 = visio.ActivePage.DrawRectangl

我正在win32com.client上使用Python 2.7,并试图找出如何更改Microsoft Visio 2013形状的字体名称和颜色

下面的代码在已打开的Visio文档上创建一个矩形。这段代码正在运行,设置形状颜色、文本和线宽没有任何问题

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"
尝试了不同的方法来更改字体名称和字体颜色,这会导致错误消息

这两行代码都会导致此错误消息:pywintypes.com_error:(-2147352567,“发生异常”),(0,u'Drawing4-Visio Standard',u'\n\n非预期的文件结尾',无,0,-2032466967),无)

接下来的三行代码都产生了类似的错误消息,但没有出现文件结尾错误:pywintypes.com_error:(-2147352567,“发生异常”),(0,u'Drawing4-Visio Standard',u'\n\nNAME?',None,0,-2032466907),None)

多次尝试导致:DrawRectangle.xxxxx无法设置

vsoShape1.fontName = "Courier"   
vsoShape1.Bold = True
vsoShape1.Bold = 1

这将设置颜色和字体

# Microsoft Office Visio Constants
visCharacterFont = 0
visCharacterColor = 1
visSectionCharacter = 3
visCharacterDblUnderline = 8
visSectionFirstComponent = 10
设置文本颜色

vsoShape.CellsSRC(visSectionCharacter, 0, visCharacterColor).FormulaU = "THEMEGUARD(RGB(0,0,0))"
设置字体

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

字体的数字描述为“一个整数,表示系统上安装的字体集合的索引。零(0)表示默认字体”。文档没有说明这个整数是始终相同还是根据安装的字体而不同。通过运行宏并查看VB脚本的输出,我得到了这个数字。

我看到您尝试了不同的方法来将文本加粗,但没有成功。我找到了一个解决方案,我将把它和其他样式选项一起发布。把它们全部弄清楚是很烦人的,因为几乎没有任何清晰的文档,所以我希望这能帮助别人

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 )

在发布之后,我确实发现了一个关于在Excel中更改字体颜色的类似问题,但没有看到关于更改字体名称的任何其他问题。一个额外的:如何添加工具提示。我花了很长时间才发现必须像Excel中那样以公式形式提供字符串<代码>形状.cellsrc(vis.visSectionObject,vis.visRowMisc,vis.visComment)。公式='=“工具提示”
vsoShape.CellsSRC(visSectionCharacter, 0, visCharacterFont).FormulaU = 100
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 )