Search code examples
pythoncomvisiowin32com

Save Visio Document as HTML


I'm trying to convert a lot of Visio files from .vsd to .html, but each file has a lot of pages, so I need to convert all pages to a single .html file.

Using the Python code below, I'm able to convert to PDF, but what I really need is HTML. I noticed I can use win32com.client.Dispatch("SaveAsWeb.VisSaveAsWeb"), but how to use it? Any ideas?

import sys
import win32com.client

from os.path import abspath


f = abspath(sys.argv[1])

visio = win32com.client.Dispatch("Visio.InvisibleApp")
doc = visio.Documents.Open(f)
doc.ExportAsFixedFormat(1, '{}.pdf'.format(f), 0, 0)

visio.Quit()
exit(0)

Solution

  • Visio cannot do that. You cannot "convert all pages into a single HTML file". You'll have a "root" file and a folder of "supporting" files.

    VisSaveAsWeb is pretty well documented, no need to guess: https://msdn.microsoft.com/en-us/vba/visio-vba/articles/vissaveasweb-object-visio-save-as-web

    -- update

    With python, it turned out to be not that trivial to deal with SaveAsWeb. It seems to default to a custom interface (non-dispatch). I don't think it's possible deal with this using win32com library, but with comtypes seems to work (comtypes library is building the client based on the type library, i.e. it also supports "custom" interfaces):

    import sys
    import comtypes
    
    from comtypes import client
    from os.path import abspath
    
    f = abspath(sys.argv[1])
    
    visio = comtypes.client.CreateObject("Visio.InvisibleApp")
    doc = visio.Documents.Open(f)
    
    comtypes.client.GetModule("{}\\SAVASWEB.DLL".format(visio.Path))
    
    saveAsWeb = visio.SaveAsWebObject.QueryInterface(comtypes.gen.VisSAW.IVisSaveAsWeb)
    webPageSettings = saveAsWeb.WebPageSettings.QueryInterface(comtypes.gen.VisSAW.IVisWebPageSettings)
    
    webPageSettings.TargetPath = "{}.html".format(f)
    webPageSettings.QuietMode = True
    
    saveAsWeb.AttachToVisioDoc(doc)
    saveAsWeb.CreatePages()
    
    visio.Quit()
    exit(0)
    

    Other than that, you can try "command line" interface: http://visualsignals.typepad.co.uk/vislog/2010/03/automating-visios-save-as-web-output.html

    import sys
    import win32com.client
    
    from os.path import abspath
    
    f = abspath(sys.argv[1])
    
    visio = win32com.client.Dispatch("Visio.InvisibleApp")
    doc = visio.Documents.Open(f)
    visio.Addons("SaveAsWeb").Run("/quiet=True /target={}.htm".format(f))
    
    visio.Quit()
    exit(0)
    

    Other than that you could give a try to my visio svg-export :)