【问题标题】:How to convert a .pptx to .pdf using Python如何使用 Python 将 .pptx 转换为 .pdf
【发布时间】:2015-10-07 20:42:26
【问题描述】:

我一直在寻找通过 Python 脚本将 .pptx 文件转换为 .pdf 文件几个小时,但似乎没有任何效果。

我尝试过的: 我尝试过 1) this script 调用 windows32.client 和 2) unoconv,但它们似乎都不适合我。

遇到的问题:使用第一个选项中的脚本会引发错误 (com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024894), None)),而在第二个选项中 Python 似乎无法识别 unoconv,即使使用 pip 安装它。

我也看到了一些推荐的Pandoc,但是我不明白如何将它用于Python。

我正在使用的版本: Python 2.7.9、Windows 8.1

【问题讨论】:

  • 我已经好几年没有用 VBA 编码了。我试图查看我拥有的一些旧代码,但找不到访问文件系统所做的工作。
  • 尝试在堆栈交换超级用户上重新提出此问题,并将其重新构建为 VBA 问题。我在那里看到了更多的 VBA 问题。
  • 感谢您的建议
  • 也试试这个帖子。将 Python 代码写入 VBA 有很多相似之处。你只需要学习对象模型中的一些对象,如果你已经足够先进,可以应对这样的挑战,那应该不会超过几个小时。 stackoverflow.com/questions/25526335/…
  • @AMR:我在comtypesthis post 的帮助下解决了这个问题。

标签: python windows pdf powerpoint converters


【解决方案1】:

我在this postthis question 的帮助下找到了答案。

请注意,comtypes 仅适用于 Windows。其他平台不支持。

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

【讨论】:

【解决方案2】:

unoconv 是执行此任务的好工具,它确实是用 python 构建的。 关于您的问题,可能与安装后在主 unoconv 文件中设置 python 解释器的方式反复出现问题有关。

要使用 python3 解释器运行它,请将 unoconv 文件 (/usr/bin/unoconv) 中的 #!/usr/bin/env python 替换为 #!/usr/bin/env python3#!/usr/bin/python3

一个班轮:

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

您还可以将/usr/bin/unoconv 符号链接到/usr/local/bin/unoconv

【讨论】:

    【解决方案3】:

    我正在使用此解决方案,但我需要搜索所有 .pptx、.ppt,然后将它们全部转换为 .pdf (python 3.7.5)。希望它有效...

    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
    

    尝试和例外是针对那些我无法阅读并且直到最后一个文档才会退出代码的文档。我建议将每种类型的格式放在一边:首先是 .pptx,然后是 .ppt(反之亦然)。

    【讨论】:

    • 这可行,但是如果文件名 (file_v_1.3.pptx) 中有一个点 (.),则此方法会产生问题。解决方法是先重命名文件,然后再重命名。有更好的方法吗?
    【解决方案4】:

    我认为必须更新答案,因为comtypes 不再有效。

    所以这是有效的代码(已接受答案的更新版本):

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

    【讨论】:

      【解决方案5】:

      看看下面的sn-p。它使用 unoconv 并且预期在 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)
      

      这个sn-p可以用于不同2的格式转换。

      Original Snippet

      【讨论】:

      • 运行这个 sn-p 时我似乎没有得到输出。我应该在哪里可以找到创建的 pdf?
      【解决方案6】:

      我需要一种将 PPTX 文件保存为 PDF 和带有注释的 PDF 的方法。这是我的解决方案

      from comtypes.client import CreateObject, Constants
      
      def PPTtoPDF(inputFileName, outputFileName, formatType = 32):
          powerpoint = CreateObject('Powerpoint.Application')
          constants = Constants(powerpoint)
          powerpoint.Visible = 1
      
          if outputFileName[-3:] != 'pdf':
              outputFileName = outputFileName + ".pdf"
          deck = powerpoint.Presentations.Open(inputFileName)
          deck.SaveAs(outputFileName, constants.PpSaveAsPDF)
          deck.Close()
          powerpoint.Quit()
      
      
      def PPTtoPDFNote(inputFileName, outputFileName, formatType = 32):
          powerpoint = CreateObject('Powerpoint.Application')
          constants = Constants(powerpoint)
          powerpoint.Visible = 1
      
          if outputFileName[-3:] != 'pdf':
              outputFileName = outputFileName + ".pdf"
          deck = powerpoint.Presentations.Open(inputFileName)
          deck.ExportAsFixedFormat(
              outputFileName,
              constants.ppFixedFormatTypePDF,
              constants.ppFixedFormatIntentPrint,
              False, # No frame
              constants.ppPrintHandoutHorizontalFirst,
              constants.ppPrintOutputNotesPages,
              constants.ppPrintAll
          )
          deck.Close()
          powerpoint.Quit()
      

      要使用它,

      PPTtoPDF    ('.\\Test.pptx', '.\Test.pdf'          )
      PPTtoPDFNote('.\\Test.pptx', '.\Test_with_Note.pdf')
      

      注意:最好使用 Windows 平台,即使用comtypes,以便始终支持 Microsoft Powerpoint 中的新格式和新功能。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-10-23
        • 2021-05-31
        • 1970-01-01
        • 2016-07-16
        • 2018-01-01
        • 1970-01-01
        相关资源
        最近更新 更多