How to convert a .pptx to .pdf using Python

前端 未结 5 585
我寻月下人不归
我寻月下人不归 2021-02-05 22:53

I have been looking to convert a .pptx file to a .pdf file through a Python script for several hours but nothing seems to be working.

What I have tried:

相关标签:
5条回答
  • 2021-02-05 23:02

    Have a look at the following snippet. It uses unoconv and it's working ex expected on UBUNTU 20.04.

    # requirements
    # sudo apt install unoconv
    # pip install tqdm
    # pip install glob
    import glob
    import tqdm
    path = "<INPUT FOLDER>"
    extension = "pptx"
    files = [f for f in glob.glob(path + "/**/*.{}".format(extension), recursive=True)]
    for f in tqdm.tqdm(files):
        command = "unoconv -f pdf \"{}\"".format(f)
        os.system(command)
    

    This snippet can be used for different-2 format conversion.

    Original Snippet

    0 讨论(0)
  • 2021-02-05 23:07

    I was working with this solution but I needed to search all .pptx, .ppt, and then turn them all to .pdf (python 3.7.5). Hope it works...

    import os
    import win32com.client
    
    ppttoPDF = 32
    
    for root, dirs, files in os.walk(r'your directory here'):
        for f in files:
    
            if f.endswith(".pptx"):
                try:
                    print(f)
                    in_file=os.path.join(root,f)
                    powerpoint = win32com.client.Dispatch("Powerpoint.Application")
                    deck = powerpoint.Presentations.Open(in_file)
                    deck.SaveAs(os.path.join(root,f[:-5]), ppttoPDF) # formatType = 32 for ppt to pdf
                    deck.Close()
                    powerpoint.Quit()
                    print('done')
                    os.remove(os.path.join(root,f))
                    pass
                except:
                    print('could not open')
                    # os.remove(os.path.join(root,f))
            elif f.endswith(".ppt"):
                try:
                    print(f)
                    in_file=os.path.join(root,f)
                    powerpoint = win32com.client.Dispatch("Powerpoint.Application")
                    deck = powerpoint.Presentations.Open(in_file)
                    deck.SaveAs(os.path.join(root,f[:-4]), ppttoPDF) # formatType = 32 for ppt to pdf
                    deck.Close()
                    powerpoint.Quit()
                    print('done')
                    os.remove(os.path.join(root,f))
                    pass
                except:
                    print('could not open')
                    # os.remove(os.path.join(root,f))
            else:
                pass
    

    The try and except was for those documents I couldn't read and won't exit the code until the last document. And I would recommend doing each type of format aside: first .pptx and then .ppt (or viceversa).

    0 讨论(0)
  • 2021-02-05 23:07

    unoconv is a great tool to perform this task and it is indeed build in python. Regarding your problem, it might be related to a recurring problem with the way the python interpreter is set in the main unoconv file after it has been installed.

    To run it with python3 interpreter, replace #!/usr/bin/env python with #!/usr/bin/env python3 or #!/usr/bin/python3 in unoconv file (/usr/bin/unoconv).

    one liner:

    sudo sed -i -e '1s:#!/usr/bin/env python$:#!/usr/bin/env python3:' /usr/bin/unoconv
    

    You could also symlink /usr/bin/unoconv to /usr/local/bin/unoconv.

    0 讨论(0)
  • 2021-02-05 23:08

    I believe the answer has to be updated because because comtypes doesn't work anymore.

    So this is the code which works (updated version of the accepted answer) :

    import win32com.client
    
    def PPTtoPDF(inputFileName, outputFileName, formatType = 32):
        powerpoint = win32com.client.DispatchEx("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()
    
    0 讨论(0)
  • 2021-02-05 23:09

    I found the answer with the help of this post and the answer from this question.

    Note that comtypes is only available for Windows. Other platforms will not support this.

    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()
    
    0 讨论(0)
提交回复
热议问题