问题描述
我一直在寻找通过Python脚本将.pptx文件转换为.pdf文件的方法,但这已经没有什么用了.
我尝试过的事情:我尝试过的事情1)此脚本,它调用Windows32.client和2) unoconv ,但似乎没有一个对我有用.
遇到的问题:从第一个选项使用脚本会引发错误(com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024894), None)
),而在第二个选项中,即使使用pip安装了unoconv
,Python似乎也无法识别. /p>
我还看到了一些推荐的 Pandoc ,但是我不明白如何在Python中使用它.
我使用的版本: Python 2.7.9,Windows 8.1
我在这篇文章,以及.
请注意,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()
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: I have tried 1) this script which calls windows32.client, and 2) unoconv, but none of them seem to be working for me.
Problems encountered: Using script from first option throws up an error (com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024894), None)
), whereas in second option Python can't seem to recognize unoconv
even after installing it using pip.
I also saw some recommended Pandoc, but I can't understand how to use it for Python.
Versions I am using: Python 2.7.9, Windows 8.1
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()
这篇关于如何使用Python将.pptx转换为.pdf的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!