我搜索了一些代码,通过Python创建Visio文档。我想添加形状,并有超链接。这样您就可以单击形状,或者最好单击形状内的文本,然后转到URL。
import os
import win32com.client
from win32com.client import constants
appVisio = win32com.client.Dispatch("Visio.Application")
appVisio.Visible =1
doc = appVisio.Documents.Add("Basic Diagram.vst")
pagObj = doc.Pages.Item(1)
stnObj = appVisio.Documents("Basic Shapes.vss")
mastObj = stnObj.Masters("Rectangle")
shpObj1 = pagObj.Drop(mastObj, 4.25, 5.5)
shpObj1.Text = "This is some text."
shpObj2 = pagObj.Drop(mastObj, 2, 2)
shpObj2.Text = """This is some more text. {\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}}"""
connectorMaster = appVisio.Application.ConnectorToolDataObject
connector = pagObj.Drop(connectorMaster, 0, 0)
connector.Cells("BeginX").GlueTo(shpObj1.Cells("PinX"))
connector.Cells("EndX").GlueTo(shpObj2.Cells("PinX"))
doc.SaveAs(r'C:\utils\MyDrawing.vsd')
doc.Close()
appVisio.Visible =0
appVisio.Quit()
RTF链接被忽略 - 我试过了。 Visio可以在UI中添加超链接。那么......有谁知道如何在这里通过Python添加链接?
答案 0 :(得分:1)
Visio仅支持形状上的链接(而不是文本本身内的链接)。形状具有Hyperlinks个Hyperlink个对象的集合,因此您可以按如下方式添加:
# shpObj2.Text = """This is some more text. {\field{\*\fldinst HYPERLINK "http://www.google.com/"}{\fldrslt http://www.google.com}}"""
shpObj2.Text = "This is a shape with multiple links."
shp2Hyperlink1 = shpObj2.Hyperlinks.Add()
shp2Hyperlink1.Name = "Google"
shp2Hyperlink1.Address = "http://www.google.com"
shp2Hyperlink2 = shpObj2.Hyperlinks.Add()
shp2Hyperlink2.Name = "BBC"
shp2Hyperlink2.Address = "http://www.bbc.co.uk"