python 操作ppt转换为pdf

时间:2019-06-24 00:09:18   收藏:0   阅读:248

使用python操作ppt转换为pdf

最近拿到了一些加密的ppt文档,只能以只读的方式打开,所以就不能编辑了,也不能直接转换为pdf文档了,需要做一些转换了。

技术分享图片

1. 需要使用WPS 2007版的(必须是2007版的才有这个功能)将ppt文档转化为可以编辑的ppt文档。

2. (1)可以直接使用WPS或者Power Point将可以编辑的ppt文档转换为PDF文档。

    (2)可以使用python操作ppt文档批量转化ppt为PDF文档。

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. 把代码和ppt文档放在同一个目录下,在终端运行python程序,就可以将ppt文档转换为pdf

技术分享图片

 

原文:https://www.cnblogs.com/homle/p/11074760.html

评论(0
© 2014 bubuko.com 版权所有 - 联系我们:wmxa8@hotmail.com
打开技术之扣,分享程序人生!