11 python pdf powerpoint converter
我一直在寻找通过Python脚本将.pptx文件转换为.pdf文件几个小时,但似乎没有任何工作.
我试过的:我试过1)这个调用windows32.client的脚本,以及2)unoconv,但它们似乎都不适合我.
遇到的问题:使用第一个选项中的脚本会抛出一个错误(com_error: (-2147352567, 'Exception occurred.', (0, None, None, None, 0, -2147024894), None)
),而在第二个选项中,unoconv
即使在使用pip安装后,Python似乎也无法识别.
我也看到了一些推荐的Pandoc,但我无法理解如何将它用于Python.
我正在使用的版本: Python 2.7.9,Windows 8.1
小智 17
我在这篇文章的帮助下找到了答案以及这个问题的答案.请注意,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()
Run Code Online (Sandbox Code Playgroud)
我正在使用这个解决方案,但我需要搜索所有 .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
Run Code Online (Sandbox Code Playgroud)
try 和 except 是针对那些我无法阅读的文档,并且在最后一个文档之前不会退出代码。我建议将每种类型的格式放在一边:首先是 .pptx,然后是 .ppt(反之亦然)。
归档时间: |
|
查看次数: |
10410 次 |
最近记录: |