【问题标题】:Error when converting Excel document to pdf using comtypes in Python使用 Python 中的 comtypes 将 Excel 文档转换为 pdf 时出错
【发布时间】:2018-03-08 18:34:36
【问题描述】:

我正在尝试使用 Python 将 Excel 电子表格转换为 PDF,并使用以下代码将 comtypes 包转换为:

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

上面的这个脚本运行良好,并且创建了 pdf 文件,但是当我尝试打开它时,我收到错误“无法打开文件 - 文件格式有问题”(但在关闭此错误对话框后实际上可以预览pdf文件)。我尝试了一个类似的脚本来将 Word 文档转换为 pdf,效果很好。

关于如何解决文件格式错误问题的任何想法?

【问题讨论】:

    标签: python excel comtypes


    【解决方案1】:

    找到了解决方案 - 这似乎有效:

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

    此链接也可能有助于启发 ExportAsFixedFormat 函数的参数:Document.ExportAsFixedFormat Method(尽管必须稍微修改一些参数值)。

    【讨论】:

    • 很好的解决方案。 FileFormat enumeration doesn't contain any PDF equivalent,所以不确定你从哪里得到 17 值(可能这是在不同应用程序的对象模型中枚举的常量,like Word?)在 Excel 中,使用 SaveAs 对话框保存为 PDF 实际上会调用ExportAsFixedFormat 方法在幕后,而不是 SaveAs 方法,可能是上面提到的原因:)
    • 好的 - 感谢您的评论。关于17... 我刚从here 那里得到它。但该数字可能来自您发布的链接。
    • 是的,这是有道理的,我只是添加一些说明,以防它对您(或任何其他偶然发现此问题的人)有所帮助。 Word/Excel/PowerPoint/等之间的常数值是不一样的,所以在一个应用程序中有效的东西(通常,但不总是)在其他应用程序中无效。无论如何,很好地解决您自己的问题!
    【解决方案2】:

    您需要描述 ExportAsFixedFormat(0,outputfile) 以将工作簿保存为 pdf 格式。 http://thequickblog.com/convert-an-excel-filexlsx-to-pdf-python/ 的解决方案对我有用。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-07
      • 2018-12-31
      • 2014-08-02
      • 2022-11-30
      • 2019-10-18
      • 2015-03-21
      • 2019-04-04
      • 1970-01-01
      相关资源
      最近更新 更多