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

标签 python pdf converters powerpoint comtypes

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

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

错误:

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 来说工作得非常好。有人知道我做错了什么吗?

最佳答案

考虑此解决方案,详细信息如下: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()

关于python - 使用 comtypes.client 将 .pptx 文件转换为 .pdf 时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45316940/

相关文章:

Javascript - GPS DMS格式转十进制格式函数

python - 使用 ValueError : A 2-dimensional array must be passed 定义复数列表中的最短对

python - 如何在spark中按多个键分组?

python - 我在将列表作为自动化函数的字符串执行时遇到问题

html - wkhtmltopdf 没有考虑 --user-style-sheet 命令行参数

java - 从 html 行更改为字符串

mysql - 从日期字符串中提取日期编号

python - pep8 在 Eclipse 中不工作

java - PDFBox 中的合并文档没有空白页或太多...但仅在打印时

python - 带有 "for i in X"的罗马数字计算器