https://zhuanlan.zhihu.com/p/365509637
https://learn.microsoft.com/zh-cn/office/vba/api/visio.selection.select
import win32com.client as win32
from win32com.client import constants
appVisio = win32.gencache.EnsureDispatch("Visio.Application")
# vdoc = appVisio.Documents.Add("Basic Diagram.vst")# 新建一个visio文件
Doc_list = appVisio.Documents.GetNames()
print(Doc_list)
vdoc = appVisio.Documents(Doc_list[0])#打开模板
page = vdoc.Pages.Item(1) # 选择第一页
# 从模板中选择一种形状
# master = stn.Masters.ItemU("Rectangle")
# master2 = stn.Masters.ItemU("Circle")
# # # 查询库中形状的名字
# # # cnt =0
# # # for elem in stn.Masters:
# # # cnt+=1
# # # if(cnt>10):
# # # break
# # # print(elem.NameU)
# # #在页面中添加形状
# shp = page.Drop(master, 0, 0)
# shp2 = page.Drop(master2,1,1)
# # # 调整shape的形状
# # shp2.Resize(constants.visResizeDirW,100, constants.visMillimeters)
# sh1 = page.DrawRectangle(1, 1, 2, 2)
# sh1.Text = "DrawRectangle"
# sh1.LineStyle = 0
# print(shp.Shapes)
# print(shp2.Shapes)
shps = vdoc.Pages(1).Shapes
print(len(shps))
for shp in shps:
# shp.Text = "xxx"
# shp.Cells("LineColor").FormulaU = 0
shp.Cells("LineWeight").FormulaU = "0.25 pt"
shp.FillStyle = "None"
shp.Text = "This is a test"
# shp.Cells("Char.size").FormulaU = "12 pt"
# shp.Cells("Char.color").FormulaU = "0"
print('shp.Text=',shp.Text)
print('shp.TextStyle',shp.TextStyle)
# shp.AddToContainers()
# shp.CellsU( 'Fillforegnd' ).FormulaU = 'RGB(0,0,255)'
# shp.FillStyle = "正常"
print('shp.FillStyle',shp.FillStyle )
print(shp.CellsU( 'Fillforegnd' ).FormulaU )
master= vdoc.Masters.ItemU(1)
win = appVisio.ActiveWindow
for shp in shps:
win.Select(shp,2)
sh3 = page.DropContainer(master,win.Selection )
# sh3.Cells("LineColor").FormulaU = 0
sh3.Cells("LineWeight").FormulaU = "0.25 pt"
sh3.FillStyle = "None"
sh3.Text = "This is a test"
# sh3.Cells("Char.size").FormulaU = "12 pt"
# sh3.Cells("Char.color").FormulaU = "0"
# 保存文件
# vdoc.SaveAs(new_file_path+'/test.vsdx')