gpt4 book ai didi

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

转载 作者:太空狗 更新时间:2023-10-30 01:14:06 33 4
gpt4 key购买 nike

我将 Python 2.7 与 win32com.client 一起使用,并试图找出如何更改 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, 'Exception occurred.', (0, u'Drawing4 - Visio Standard', u'\n\n文件意外结束。 ', 无, 0, -2032466967), 无)

vsoShape1.Cells("Font.Name").FormulaU = "Courier"
vsoShape1.Cells("Font.Bold").FormulaU = "True"

接下来的三行代码都导致类似的错误消息,没有文件结束错误:pywintypes.com_error: (-2147352567, 'Exception occurred.', (0, u'Drawing4 - Visio Standard', u'\n\nNAME?', 无, 0, -2032466907), 无)

vsoShape1.Cells("Char.Font").FormulaU = "Courier"
vsoShape1.Cells("Char.colorIndex").FormulaU = 16
vsoShape1.Cells("Font.Bold").FormulaU = 0

又尝试了几次导致:无法设置DrawRectangle.xxxxx。

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

最佳答案

我看到您尝试了不同的方法来使文本变粗,但没有成功。我找到了一个解决方案,我将把它与其他样式选项一起发布。弄清楚它们很烦人,因为几乎没有任何明确的文档,所以我希望这会对某人有所帮助。

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 )

关于python - 如何使用 Python 更改 Visio 字体名称和颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32343086/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com