Skip to content
Advertisement

Print Excel to pdf with xlwings

I am trying to print Excel files to pdf with xlwings. I am using the excel api for this.

I have tried it in two ways:

1/ Using the PrintOut() call with PrintToFile argument:

JavaScript

The problem here is Excel just prints the file, ignoring my additional settings.

2/ Using ExportAsFixedFormat

JavaScript

Here Excel flashes a bit, but does not do anything in the end.

For the record: I can’t use a macro and call it from Python because I have about a thousand of these Excel files. So, I can’t put the macro in every single one of them. It would probably be a workaround to create a custom function in VBA and than call it every file. But, honestly, it would be easier if I could just do this directly from Python, in one line of code.

Advertisement

Answer

Below is a self-standing code example of what worked on my machine to print an excel workbook to pdf (using the ExportAsFixedFormat method):

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement