【问题标题】:convert ppt to PDF (error encountered)将 ppt 转换为 PDF(遇到错误)
【发布时间】: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


【解决方案1】:

由于您是在 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# 互操作库,但我认为这需要安装 Microsoft Office。

谢谢。

【讨论】:

    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 2021-01-13
    • 1970-01-01
    • 1970-01-01
    • 2018-06-18
    • 2016-05-17
    相关资源
    最近更新 更多