4

I'm trying to convert a .pptx file to a pdf file. So far I got:

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

Error:

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

Almost the same code works perfectly fine for Word and Excel. Has anyone an idea what I am doing wrong?

1 Answers1

1

Consider this solution as detailed here: How to convert a .pptx to .pdf using Python.

The solution from the above resource (which worked for me today) is:

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()
DonnRK
  • 597
  • 1
  • 6
  • 17
  • thanks for sharing, but would this work in a loop? i'm trying to create 2 or more pdfs out of different ppts by reusing the above code inside a for loop, but the code only works for the 1st ppt - pdf conversion. after that, i get an error "COMError: (-111111, None, ('Presentation (unknown member) : Object does not exist.', 'Microsoft PowerPoint', '', 0, None))" – Scinana Jan 20 '22 at 19:43
  • I have the same error using this solution: "_ctypes.COMError: (-2147467259, 'Error no especificado', ('Presentation (unknown member) : PowerPoint no puede guardar ^0 en ^1.', 'Microsoft PowerPoint', '', 0, None))" – David Valenzuela Urrutia Jan 31 '23 at 14:00