使用comtypes.client将.pptx文件转换为.pdf时出错

时间:2017-07-26 03:29:22

标签: python pdf converter powerpoint comtypes

我试图将.pptx文件转换为pdf文件。到目前为止,我得到了:

xprime = (x-((m+1)/2))*cos(tetav)+(y-((n+1)/2))*sin(tetav);
yprime = -(x-((m+1)/2))*sin(tetav)+(y-((n+1)/2))*cos(tetav);

错误:

PP_FORMAT_PDF = 32

def _convert_powerpoint2pdf(input_path, output_path):
    try:
        import comtypes
        import comtypes.client
    except ImportError as error:
        raise 
    else:
        powerpoint = slides = None
        try:
            comtypes.CoInitialize()
            powerpoint = comtypes.client.CreateObject('Powerpoint.Application')
            slides = powerpoint.Presentations.Open(input_path)
            slides.SaveAs(output_path, FileFormat=PP_FORMAT_PDF)
        except WindowsError as error:
            raise
        except comtypes.COMError as error:
            raise IOError(error)
        finally:
            slides and slides.Close()
            powerpoint and powerpoint.Quit()
            comtypes.CoUninitialize()

几乎相同的代码对Word和Excel完全正常。有谁知道我做错了什么?

1 个答案:

答案 0 :(得分:1)

请按照以下详细说明考虑此解决方案:How to convert a .pptx to .pdf using Python

上述资源(今天对我有用)的解决方案是:

import comtypes.client

def PPTtoPDF(inputFileName, outputFileName, formatType = 32):
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible = 1

    if outputFileName[-3:] != 'pdf':
        outputFileName = outputFileName + ".pdf"
    deck = powerpoint.Presentations.Open(inputFileName)
    deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
    deck.Close()
    powerpoint.Quit()