问题
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
回答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.
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 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
.
来源:https://stackoverflow.com/questions/31487478/how-to-convert-a-pptx-to-pdf-using-python