将 ppt 转换为 PDF(遇到错误)



目标:使用 python 3.6.1 将 ppt 转换为 pdf

场景:MS Office 未安装在 Windows 服务器中

使用的代码:

from subprocess import Popen, PIPE
import time
def convert(src, dst):
d = {'src': src, 'dst': dst}
commands = [
'/usr/bin/docsplit pdf --output %(dst)s %(src)s' % d,
'oowriter --headless -convert-to pdf:writer_pdf_Export %(dst)s %(src)s' % d,
]
for i in range(len(commands)):
command = commands[i]
st = time.time()
process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) # I am aware of consequences of using `shell=True` 
out, err = process.communicate()
errcode = process.returncode
if errcode != 0:
raise Exception(err)
en = time.time() - st
print ('Command %s: Completed in %s seconds' % (str(i+1), str(round(en, 2))))
if __name__ == '__main__':
src = 'C:xxxppt'
dst = 'C:xxxpptdestination'
convert(src, dst)

遇到错误:

Traceback (most recent call last):
File "C:/PythonFolder/ppt_to_pdf.py", line 134, in <module>
convert(src, dst)
File "C:/PythonFolder/ppt_to_pdf.py", line 123, in convert
process = Popen(command, stdout=PIPE, stderr=PIPE, shell=True) # I am aware of consequences of using `shell=True`
File "C:Python 3.6.1libsubprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:Python 3.6.1libsubprocess.py", line 990, in _execute_child
startupinfo)
ValueError: embedded null character

有谁知道如何解决此错误? 或任何其他在这种情况下会有所帮助的 python 库。

由于您在Windows上运行,因此命令/usr/bin/docsplit pdf --output %(dst)s %(src)s不会转换PPT,因为它似乎适用于Linux。Popen 可能在处理该命令时遇到问题,从而导致错误。

在Windows上的命令行中将PPT转换为PDF有点困难。我认为你最好的选择是安装LibreOffice并以无头模式运行。还有一个超级用户问题,提问者最终使用 C# 互操作库,但我认为这需要安装 Microsoft Office。

谢谢。

最新更新