【发布时间】:2017-08-06 18:35:53
【问题描述】:
目标:使用python 3.6.1将ppt转换为pdf
场景:Windows 服务器中没有安装 MS Office
使用的代码:
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:\xxx\ppt'
dst = 'C:\xxx\ppt\destination'
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.1\lib\subprocess.py", line 707, in __init__
restore_signals, start_new_session)
File "C:\Python 3.6.1\lib\subprocess.py", line 990, in _execute_child
startupinfo)
ValueError: embedded null character
有谁知道如何解决这个错误? 或在这种情况下有帮助的任何其他 python 库。
【问题讨论】:
-
您是在 Windows 还是 Linux 上运行?
-
如果您在 Windows 上运行,我认为命令
/usr/bin/docsplit pdf --output %(dst)s %(src)s不会转换 PPT,因为它似乎是针对 Linux 的。 Popen 可能无法处理该命令,从而导致该错误。 -
我在 Windows 上运行。如果是这种情况,有什么解决方法吗?
-
检查我的答案。
标签: python python-3.x pdf subprocess powerpoint