반응형
아시는 바와 같이 Microsoft Office 제품군에서는 VBA를 활용하여 자동화가 가능 합니다.
http://iamaman.tistory.com/592
http://iamaman.tistory.com/1643
그런데 Visual Basic뿐만 아니라 다른 언어를 활용해서도 Office 자동화가 가능 합니다. 오늘은 Python 을 이용한 visio 그리기를 해보려 합니다.
구글을 좀 뒤져 보니 다음과 같이 좋은 예가 있더군요.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
from win32com.client import constants | |
import win32com.client | |
win32com.client.gencache.EnsureDispatch("Visio.Application") | |
visapp = win32com.client.Dispatch("Visio.Application") | |
visapp.Visible = False | |
doc = visapp.Documents.Add("") | |
page = visapp.ActivePage | |
stencilname = "basic_u.vss" | |
stencildocflags = constants.visOpenRO | constants.visOpenDocked | |
stencildoc = visapp.Documents.OpenEx(stencilname , stencildocflags) | |
masterrect = stencildoc.Masters.ItemU("Rounded rectangle") | |
mastercircle = stencildoc.Masters.ItemU("circle") | |
masterconnector = stencildoc.Masters.ItemU("dynamic connector") | |
shape1 = page.Drop(masterrect, 0, 0) | |
for i in range(2): | |
shape2 = page.Drop(mastercircle, 3 + 4 * i, 3 + 2 * i) | |
connector = page.Drop(masterconnector, -1, -1) | |
connector.CellsU("BeginX").GlueTo(shape1.Cells("Geometry1.X1")) | |
connector.CellsU("BeginY").GlueTo(shape1.Cells("Geometry1.Y1")) | |
connector.CellsU("EndX").GlueTo(shape2.Cells("Geometry1.X2")) | |
connector.CellsU("EndY").GlueTo(shape2.Cells("Geometry1.Y2")) | |
shape1 = shape2 | |
doc.SaveAs(r'C:\MyDrawing.vsd') | |
visapp.Quit() |
저는 현재 Windows 7 64비트를 사용 중인데~ Python 은 32비트를 사용 중입니다. 몇가지 package 들을 사용하기 위해 아래에서도 소개했던 Anaconda 를 사용하는데요~ Anaconda 를 설치하니 win32com 모듈을 그냥 사용 할 수 있더군요.
http://iamaman.tistory.com/1610
http://iamaman.tistory.com/1001
http://iamaman.tistory.com/1648
위 코드의 결과 아래와 같은 Visio 다이어그램이 나오게 됩니다.
좀만 공부해서 응용해 보면 다양한 다이어 그림들을 자동화 할 수 있을 것 같네요.
반응형
'programming language > Python' 카테고리의 다른 글
Python 다차원 배열 또는 튜플에서 특정 열의 값 추출하기 (0) | 2017.02.16 |
---|---|
Mint Linux 에서 Python C 확장 모듈 사용 방법 (0) | 2016.12.19 |
윈도우 Python 에서 R 함수를 사용하기 위해 rpy2 설치하기 (0) | 2016.01.13 |
Python Home folder 접근 (0) | 2015.10.11 |
Python Degree to Clock 변환 (0) | 2015.09.13 |
파이썬으로 윈도우 아웃룩 메일 보내기 (0) | 2015.08.26 |
Python에서 Autohotkey 사용하기 (5) | 2015.08.06 |
chardet 을 활용한 텍스트 파일 encoding 확인 (0) | 2015.05.08 |
댓글