Welcome, guest | Sign In | My Account | Store | Cart

Convert graphs in the DOT format to SVG on Windows.

Python, 27 lines
 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.

4 comments

David Eppstein 18 years, 1 month ago  # | flag

Is it really appropriate to put such platform-specific code here?

Klaus Muller 17 years, 12 months ago  # | flag

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!

Michael Cochez 14 years, 4 months ago  # | flag

platform independent form. #on other systems graphiz can be used as commandline tool, so use os.system

import sys
if sys.platform == "win32":
    from win32com.client import Dispatch
    def toSVG(filename, target =""):
        graphViz=Dispatch("WINGRAPHVIZ.dot")
        f=open(filename,'r')
        data = f.read()
        f.close()
        img=graphViz.toSVG(data)
        if target == "" :
            target = filename+".svg"
        f=open(target,'w')
        f.write(img)
        f.close()
else:
#there might be a need for further specifying on other platforms
    import os
    def toSVG(fileName, target = ""):
        if target == "" :
            target = filename+".svg"
        command = "dot -Tsvg " + fileName + " -o "+ target #Tsvg can be changed to Tjpg, Tpng, Tgif etc (see dot man pages)
        os.system(command)
if __name__=="__main__":
    if len(sys.argv)#on other systems graphiz can be used as commandline tool, so use os.system
<pre>import sys
if sys.platform == "win32":
    from win32com.client import Dispatch
    def toSVG(filename, target =""):
        graphViz=Dispatch("WINGRAPHVIZ.dot")
        f=open(filename,'r')
        data = f.read()
        f.close()
        img=graphViz.toSVG(data)
        if target == "" :
            target = filename+".svg"
        f=open(target,'w')
        f.write(img)
        f.close()
else:
#there might be a need for further specifying on other platforms
    import os
    def toSVG(fileName, target = ""):
        if target == "" :
            target = filename+".svg"
        command = "dot -Tsvg " + fileName + " -o "+ target #Tsvg can be changed to Tjpg, Tpng, Tgif etc (see dot man pages)
        os.system(command)
if __name__=="__main__":
    if len(sys.argv)

</pre>

Michael Cochez 14 years, 4 months ago  # | flag

something goes wrong when pasting code, changed '<' to '!='

import sys
if sys.platform == "win32":
    from win32com.client import Dispatch
    def toSVG(filename, target =""):
        graphViz=Dispatch("WINGRAPHVIZ.dot")
        f=open(filename,'r')
        data = f.read()
        f.close()
        img=graphViz.toSVG(data)
        if target == "" :
            target = filename+".svg"
        f=open(target,'w')
        f.write(img)
        f.close()
else:
#there might be a need for further specifying on other platforms
    import os
    def toSVG(fileName, target = ""):
        if target == "" :
            target = filename+".svg"
        command = "dot -Tsvg " + fileName + " -o "+ target #Tsvg can be changed to Tjpg, Tpng, Tgif etc (see dot man pages)
        os.system(command)
if __name__=="__main__":
    if len(sys.argv) != 2:
        print "Usage: python dot2svg.py mygraph.dot"
        exit ()
    filename=sys.argv[1]
    toSVG(filename)