Reputation: 51
I would like to create a script that opens Visio files (.vsd), save it to vsdx, pdf and svg (with every page of vsd being seperate file), close the file, opens the next until end of files.
So far i was successful with saving it to .pdf using: Python Visio to pdf
import win32com.client
#change later to dynamic current path
path= r"C:/automation_visio/"
visio = win32com.client.Dispatch("Visio.Application")
doc = visio.Documents.Open(path+'test.vsd')
doc.ExportAsFixedFormat( 1, path+'test.pdf', 1, 0 ) #exports as pdf only XD
I looked in lots of places (most relevant: https://learn.microsoft.com/en-us/office/vba/api/visio.document.saveas) but to no avail - I don't know how to save to other filetypes which are available with manual "SaveAs".
EDIT: I need to know also how to navigate trough the pages (to get list of pages and iterate trough them and save to svg files) and (shamefully) how to correctly close the file after files are exported.
Upvotes: 2
Views: 1437
Reputation: 211
I wanted to add that there are two options for exporting SVG from Visio. Normally, Visio adds a bunch of extra data, such as User-defined cells, Layers, and Shape Data fields. This can be useful if you want to program against the export, or re-import to Visio at some time in the future.
However, if you want small, clean SVG, you won't want all of that extra stuff. So you can fiddle with Visio.Application.ApplicationSettings.SVGExportFormat, setting it equal to one of the following:
// (0) Include both SVG elements and Visio elements. This is the default.
Visio.VisSVGExportFormat.visSVGIncludeVisioElements
// (1) Include SVG elements only.
Visio.VisSVGExportFormat.visSVGExcludeVisioElements
The extra Visio info that is added to the SVG export is easy to find, just look for elements with the "v:" prefix.
Upvotes: 2
Reputation: 12245
You need to use page.Export
method instead of ExportAsFixedFormat. Just give the target file the .svg
extension, and you are good to go.
BTW, I have a Visio Add-in (check the profile) that adds some useful stuff to the export, like connections, properties, etc, to be used from JavaScript. And it is also callable programmatically.
Upvotes: 4