2

I am trying to convert an Excel spreadsheet to PDF using Python and the comtypes package using this code:

import os
import comtypes.client

FORMAT_PDF = 17
SOURCE_DIR = 'C:/Users/IEUser/Documents/jscript/test/resources/root3'
TARGET_DIR = 'C:/Users/IEUser/Documents/jscript'

app = comtypes.client.CreateObject('Excel.Application')
app.Visible = False

infile = os.path.join(os.path.abspath(SOURCE_DIR), 'spreadsheet1.xlsx')
outfile = os.path.join(os.path.abspath(TARGET_DIR), 'spreadsheet1.pdf')

doc = app.Workbooks.Open(infile)
doc.SaveAs(outfile, FileFormat=FORMAT_PDF)
doc.Close()

app.Quit()

This script above runs fine and the pdf file is created, but when I try to open it I get the error "The file cannot be opened - there is a problem with the file format" (but after closing this error dialog it is actually possible to preview the pdf file). I have tried a similar script to convert Word documents to pdfs and this worked just fine.

Any ideas on how I can resolve this problem with the file format error?

Andreas
  • 93
  • 1
  • 8

2 Answers2

3

Found a solution - this seems to be working:

import os
import comtypes.client

SOURCE_DIR = 'C:/Users/IEUser/Documents/jscript/test/resources/root3'
TARGET_DIR = 'C:/Users/IEUser/Documents/jscript'

app = comtypes.client.CreateObject('Excel.Application')
app.Visible = False

infile = os.path.join(os.path.abspath(SOURCE_DIR), 'spreadsheet1.xlsx')
outfile = os.path.join(os.path.abspath(TARGET_DIR), 'spreadsheet1.pdf')

doc = app.Workbooks.Open(infile)
doc.ExportAsFixedFormat(0, outfile, 1, 0)
doc.Close()

app.Quit()

This link may also be helpful as an inspiration regarding the arguments to the ExportAsFixedFormatfunction: Document.ExportAsFixedFormat Method (although some of the values of arguments have to be modified a bit).

Andreas
  • 93
  • 1
  • 8
  • 1
    Good solution. The [`FileFormat` enumeration doesn't contain any PDF equivalent](https://msdn.microsoft.com/en-us/vba/excel-vba/articles/xlfileformat-enumeration-excel), so not sure where you got the `17` value (possibly this is a constant enumerated in a different application's object model, [like Word](https://msdn.microsoft.com/en-us/vba/word-vba/articles/wdsaveformat-enumeration-word)?) In Excel, using the SaveAs dialog to save as a PDF actually invokes the `ExportAsFixedFormat` method behind the scenes, not the `SaveAs` method, probably for reason mentioned above :) – David Zemens Sep 27 '17 at 12:58
  • 1
    Ok - thanks for your comment. About the `17`... I just picked it up from [here](https://stackoverflow.com/questions/6011115/doc-to-pdf-using-python). But the number probably comes from the link you posted. – Andreas Sep 27 '17 at 14:52
  • yes that makes sense, I was just adding some clarification in case it would be helpful for you (or anyone else who stumbles upon this question). The constant values are not the same between Word/Excel/PowerPoint/etc., so what works in one application (often, but not always) doesn't work in others. In any case, good job solving your own question! – David Zemens Sep 27 '17 at 14:54
0

You need to describe ExportAsFixedFormat(0,outputfile) to save workbook in pdf format. The solution from http://thequickblog.com/convert-an-excel-filexlsx-to-pdf-python/ works for me.

from win32com import client
import win32api
input_file = r'C:\Users\thequickblog\Desktop\Python session 2\tqb_sample.xlsx'
#give your file name with valid path 
output_file = r'C:\Users\thequickblog\Desktop\Python session 2\tqb_sample_output.pdf'
#give valid output file name and path
app = client.DispatchEx("Excel.Application")
app.Interactive = False
app.Visible = False
Workbook = app.Workbooks.Open(input_file)
try:
    Workbook.ActiveSheet.ExportAsFixedFormat(0, output_file)
except Exception as e:
    print("Failed to convert in PDF format.Please confirm environment meets all the requirements  and try again")
    print(str(e))
finally:
    Workbook.Close()
    app.Exit()
suman
  • 367
  • 2
  • 4