Error when converting Excel document to pdf using comtypes in Python

后端 未结 2 459
隐瞒了意图╮
隐瞒了意图╮ 2021-01-15 17:03

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
S         


        
2条回答
  •  时光说笑
    2021-01-15 17:59

    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).

提交回复
热议问题