2

I'm trying to convert a bunch of Visio files to pdf in python. I have referenced this .doc to pdf using python and written the following code:

import comtypes.client as coms

format=17    
visio = coms.CreateObject('Visio.Application')
doc = visio.Documents.Open('map.vsd')
doc.SaveAs('map.pdf', FileFormat=format)

gives me a TypeError: call takes exactly 2 arguments (3 given)

I've been Googling and can't find the reference on how to print to pdf in Visio using python.

Community
  • 1
  • 1
jason
  • 3,811
  • 18
  • 92
  • 147

1 Answers1

3

You should use ExportAsFixedFormat instead SaveAs. Documentation for this function you can find here. This function can be used with a win32 and a comtypes.

win32com example

import win32com.client
visio = win32com.client.Dispatch("Visio.Application")
doc = visio.Documents.Open('map.vsd')
doc.ExportAsFixedFormat( 1, 'map.pdf', 1, 0 )

comtypes example

import comtypes.client as coms
visio = coms.CreateObject('Visio.Application')
doc = visio.Documents.Open('map.vsd')
doc.ExportAsFixedFormat( 1, 'map.pdf', 1, 0 )
NorthCat
  • 9,643
  • 16
  • 47
  • 50
  • Is there any update to this with the .vsdx format and Visio 2016? I am getting an error when running this code with that file format. Specifically, I get "(-2032466859, None, ('\n\nThis file name is not valid.', 'Visio Professional', None, 0, None))". – verybadatthis Sep 06 '16 at 22:21
  • I am getting this error - `Arguments: (com_error(-2147221005, 'Invalid class string', None, None),)` – Sankar Mar 27 '20 at 13:09