2017-08-06 87 views
0

目的:微軟Office未安裝在Windows Server轉換PPT到PDF(時遇到錯誤)

使用代碼:

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) 

使用Python 3.6.1

場景轉換的PPT到PDF遇到的錯誤:

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庫,這將有助於在這種情況下。

+0

你在Windows或Linux上運行嗎? – identicon

+0

如果你在Windows上運行,我不認爲命令'/ usr/bin/docsplit pdf --output%(dst)s%(src)s'會轉換PPT,因爲它看起來適用於Linux。 Popen可能無法處理該命令,導致該錯誤。 – identicon

+0

我正在windows上運行。如果是這種情況,是否有任何解決方法? – grc

回答

0

由於您在Windows上運行,因此命令/usr/bin/docsplit pdf --output %(dst)s %(src)s不會轉換PPT,因爲它看起來適用於Linux。 Popen可能無法處理該命令,導致錯誤。

在Windows上的命令行中將PPT轉換爲PDF格式相當困難。我認爲你最好的選擇是to install LibreOffice and run with headless mode。還有a SuperUser question on it提交者最終使用C#interop庫,但我認爲這需要安裝Microsoft Office。

謝謝。