文章目录
- 1 自动导出visio为图片
- 2 打开UML类图
visio官方文档
模板
1 自动导出visio为图片
#!/usr/bin/python
# -*- coding: GBK -*-
import logging,os,sys
import win32com.client as win32
from win32com.client import constants
from logging import debug, info#logging.basicConfig( level = logging.DEBUG, format = '[%(levelname)s] %(message)s')
logging.basicConfig( level = logging.DEBUG, format = '[%(filename)s %(lineno)d ] %(message)s')def export_visio_sheet(file,dir=None):#appVisio = win32.gencache.EnsureDispatch("Visio.InvisibleApp") #设置不可见appVisio = win32.gencache.EnsureDispatch("Visio.Application") #可见#appVisio.Visible = False #设置不可见try:fullfile = os.path.join(os.getcwd(), file )logging.info(file)vdoc = appVisio.Documents.Open(fullfile)pages_num = len( vdoc.Pages)debug( "pages {0}".format( pages_num ) )#创建目录if dir:dir = file.split('.')[0]if not os.path.exists(dir): os.mkdir(dir)for i in range(pages_num):#设置主题(无主题)# page = vdoc.Pages()# page.SetTheme(0)page_name = vdoc.Pages(i+1).Nameif dir:export_name = os.path.join(os.getcwd(),dir, "%s.png"%(page_name) )else:export_name = os.path.join(os.getcwd(),"%s.png"%(page_name) )logging.info(export_name)vdoc.Pages(i+1).Export (export_name) except Exception as e:info(e)finally:appVisio.Quit() #退出if __name__ == '__main__':export_visio_sheet("1.vsdx",dir=True)
2 打开UML类图
#!/usr/bin/env python3
# -*- coding:GBK -*-
# Author: Zeroimport win32com.client
import os, sys, logging,re
from logging import debug, infologging.basicConfig( level = logging.DEBUG, format = '[%(filename)s %(lineno)d ] %(message)s')SHAPE_CLASS = 0
SHAPE_MEMBER = 1class visioumlclass(object):def __init__(self, filename ):self.filename = filename self.pagecount = 0def open(self, name=''):if(name!=''): self.filename = os.path.join(os.getcwd(), name )info("Drawing UML Class: %s", self.filename )self.visio = win32com.client.Dispatch("Visio.Application")#self.Visio = win32com.client.Dispatch("Visio.InvisibleApp")# add a new file using visio template nameself.visio_template = self.visio.Documents.Add("USTRME_M.vst") #新建一个visio文件 UML类for doc in self.visio.Documents :#find visioStencilif (doc.Name == "USTRME_M.VSSX" or doc.Name == "USTRME_M.vssx" ):self.visioStencil = docbreak# get master and shapes# shape01 = self.visioStencil.Masters.ItemU("Class") #类# shape02 = self.visioStencil.Masters.ItemU("Member") #成员函数# shape03 = self.visioStencil.Masters.ItemU("Interface") #接口#visio 2013测试过'''shape01 = self.visioStencil.Masters.ItemU(1) #类shape02 = self.visioStencil.Masters.ItemU(2) #成员shape03 = self.visioStencil.Masters.ItemU(3) #分隔符shape04 = self.visioStencil.Masters.ItemU(4) #接口shape05 = self.visioStencil.Masters.ItemU(5) #枚举shape06 = self.visioStencil.Masters.ItemU(6) #包(展开)shape07 = self.visioStencil.Masters.ItemU(7) #包(折叠)shape08 = self.visioStencil.Masters.ItemU(8) #注释shape09 = self.visioStencil.Masters.ItemU(9) #继承shape010 = self.visioStencil.Masters.ItemU(10) #接口实现shape011 = self.visioStencil.Masters.ItemU(11) #关联shape012 = self.visioStencil.Masters.ItemU(12) #直接关联shape013 = self.visioStencil.Masters.ItemU(13) #聚合shape014 = self.visioStencil.Masters.ItemU(14) #依赖关系shape015 = self.visioStencil.Masters.ItemU(15) #复合'''self.shapes = []for i in range(1,16,1):shape = self.visioStencil.Masters.ItemU(i) self.shapes.append(shape)#self.pagecount = 1#pageself.page = self.visio_template.Pages(self.pagecount)self.page.SetTheme(0,0,0,0,0)self.pagewidth = self.page.PageSheet.CellsU("PageWidth").ResultIUself.pageheight = self.page.PageSheet.CellsU("PageHeight").ResultIUdef new_page(self, page_name ):if (self.pagecount == 0): self.open()# draw on the first pageif (self.pagecount == 1):self.page = self.visio_template.Pages.Item(self.pagecount)#设置主题(无主题)self.page.SetTheme(0, 0, 0, 0, 0)else:#添加新的pageself.page = self.visio_template.Pages.Add()self.pagecount += 1self.page.Name = page_nameself.pagewidth = self.page.PageSheet.CellsU("PageWidth").ResultIUself.pageheight = self.page.PageSheet.CellsU("PageHeight").ResultIUdebug("width : %f"%self.pagewidth )debug("height: %f"%self.pageheight )return self.pagedef draw_shape(self, type, x, y, text, linewidth=0 ):shape = self.page.Drop( self.shapes[type], 1.5*x, self.pageheight - 1.89*y)shape.Text = textreturn shapedef draw_member(self, mlist):'''绘制shape 类成员:param mlist: 成员text列表:return: 无'''for i in range(len(mlist)):#绘制成员member = self.page.Drop(self.shapes[SHAPE_MEMBER], 2, self.pageheight-1 - 0.3*i)member.Text = mlist[i]def save(self):info('saving visio %s',self.filename)self.visio.ActiveDocument.SaveAs(self.filename)self.visio.ActiveDocument.Close()self.visio.Quit()def close(self):self.visio.ActiveDocument.SaveAs(self.filename)self.visio.ActiveDocument.Close()self.visio.Quit()#绘制继承关系demodef test_draw(self):info(self.pagewidth)info(self.pageheight)shapeclass = self.page.Drop( self.shapes[0], 0, 0 )shapeclass.Text = "测试类===="shapeclass1 = self.page.Drop( self.shapes[0], 0, 2 )shapeclass1.Text = "测试类1===="line = self.page.Drop( self.shapes[8], 0, 0 )line.CellsU("BeginX").GlueTo( shapeclass.CellsU("Connections.X4") )line.CellsU("EndX").GlueTo( shapeclass1.CellsU("Connections.X3") )#shapemember = self.page.Drop( self.shapes[1], 0, 0)def test_draw1(self):for i in range(10):member = self.page.Drop(self.shapes[1], 2, 0.5*i)print(member.CellsU("Width").ResultIU)if __name__ == "__main__":test = visioumlclass( os.path.join(os.getcwd(), "uml.vsdx"))test.open()test.draw_member(["函数01","函数02","函数03","函数04"])test.test_draw()#test.save()
效果如下:
上面类似做法基本可以实现想要的功能了,但是官方文档中没有找到二维图形如何连接,希望有小伙伴能看到指导。类似将上述member添加到类中,没想到如何实现?