Convert graphs in the DOT format to SVG on Windows.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | # dot2svg.py
# Author: Simon Frost, UCSD
# Date : 17th October 2003
# Description: This is a simple demonstration of how to use
# WinGraphViz (http://home.so-net.net.tw/oodtsen/wingraphviz/index.htm)
# from Python (http://www.python.org) using Mark Hammond's win32all extensions
# (http://starship.python.net/crew/mhammond/win32/). It takes a graph in DOT
# format, and writes the graph in SVG.
from win32com.client import Dispatch
import sys
def toSVG(filename):
graphViz=Dispatch("WINGRAPHVIZ.dot")
f=open(filename,'r')
data = f.read()
f.close()
img=graphViz.toSVG(data)
f=open(str(filename)+'.svg','w')
f.write(img)
f.close()
if __name__=="__main__":
if len(sys.argv)<2:
print "Usage: python dot2svg.py mygraph.dot"
filename=sys.argv[1]
toSVG(filename)
|
I wanted a quick way of converting from the DOT format of representing a graph (which is very handy) to SVG (which looks very nice) on Windows. Using the win32all extensions, you can do this with just a call to a COM object. You'll need WinGraphViz (http://home.so-net.net.tw/oodtsen/wingraphviz/index.htm) somewhere in your PATH.
Tags: graphics
Is it really appropriate to put such platform-specific code here?
Yes, we certainly need platform-dependent recipes like this. Simon's recipe is very powerful and fills a great need for anybody working on graphs and having to render them. Why deny this to the large number of Python users on Windows?
Rather than criticizing the platform dependency, build an equivalent recipe for your platform of choice!
platform independent form. #on other systems graphiz can be used as commandline tool, so use os.system
</pre>
something goes wrong when pasting code, changed '<' to '!='