python operation ppt convert pdf

Use python operation ppt convert pdf

Recently received a number of encryption ppt files can only be opened in read-only mode, so it can not be edited, and can not be converted directly into a pdf document, you need to do some converted.

1. The need to use the 2007 version of WPS (must be the 2007 version have this feature) will ppt ppt document into a document that can be edited.

2. (1) can be used directly or WPS Power Point will be able to edit ppt document into a PDF document.

    (2) can operate using python ppt ppt document batch convert PDF document.

import comtypes.client
import os
 
def init_powerpoint():
    powerpoint = comtypes.client.CreateObject("Powerpoint.Application")
    powerpoint.Visible = 1
    return powerpoint
 
def ppt_to_pdf(powerpoint, inputFileName, outputFileName, formatType = 32):
    if outputFileName[-3:] != 'pdf':
        outputFileName = outputFileName + ".pdf"
    deck = powerpoint.Presentations.Open(inputFileName)
    deck.SaveAs(outputFileName, formatType) # formatType = 32 for ppt to pdf
    deck.Close()
 
def convert_files_in_folder(powerpoint, folder):
    files = os.listdir(folder)
    pptfiles = [f for f in files if f.endswith((".ppt", ".pptx"))]
    for pptfile in pptfiles:
        fullpath = os.path.join(cwd, pptfile)
        ppt_to_pdf(powerpoint, fullpath, fullpath)
 
if __name__ == "__main__":
    powerpoint = init_powerpoint()
    cwd = os.getcwd()
    convert_files_in_folder(powerpoint, cwd)
    powerpoint.Quit()

 3. The code and ppt files in the same directory, run python program in the terminal, you can convert the document to pdf ppt

 

Guess you like

Origin www.cnblogs.com/homle/p/11074760.html