首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用comtypes.client将.pptx文件转换为.pdf时出错

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

Stack Overflow用户
提问于 2017-07-26 11:29:22
回答 1查看 1.3K关注 0票数 2

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

代码语言:javascript
复制
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()

错误:

代码语言:javascript
复制
Traceback (most recent call last):
  File "C:/Users/Mathias/git/pdfconv/tests\test_converter.py", line 89, in test_convert_presentation2pdf_pptx
    pdfconv.converter.convert_presentation2pdf(input_path, output_path)
  File "c:\users\mathias\git\pdfconv\pdfconv\converter.py", line 116, in convert_presentation2pdf
    _convert_powerpoint2pdf(input_path, output_path)
  File "c:\users\mathias\git\pdfconv\pdfconv\converter.py", line 179, in _convert_powerpoint2pdf
    slides.SaveAs(output_path, FileFormat=PP_FORMAT_PDF)
_ctypes.COMError: (-2147467259, 'Unbekannter Fehler', (u'Presentation (unknown member) : PowerPoint kann ^0 auf ^1 nicht speichern.', u'Microsoft PowerPoint 2013', u'', 0, None))

几乎相同的代码完全适用于Word和Excel。有谁知道我做错了什么吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-08-21 23:27:09

请考虑如下所示的解决方案:How to convert a .pptx to .pdf using Python

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

代码语言:javascript
复制
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()
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45316940

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档