python – 将文档转换为pdf格式的有效方式

我一直在试图找到转换文档的有效方法,例如doc,docx,ppt,pptx到pdf。到目前为止,我已经尝试了docsplit和oowriter,但都采取> 10秒完成在pptx file的工作,大小为1.7MB。有谁可以建议我更好的方法或建议来改善我的方法?

我曾尝试过:

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 = '/path/to/source/file/'
    dst = '/path/to/destination/folder/'
    convert(src, dst)

输出:

Command 1: Completed in 11.91 seconds
Command 2: Completed in 11.55 seconds

环境:

Linux – Ubuntu 12.04
> Python 2.7.3

更多工具结果:

> jodconverter拿了11.32秒

尝试从您的Python代码调用unoconv,我的本地机器需要8秒钟,我不知道它是否足够快:

time unoconv 15.\ Text-Files.pptx
real    0m8.604s
http://stackoverflow.com/questions/20891787/an-efficient-way-to-convert-document-to-pdf-format

本站文章除注明转载外,均为本站原创或编译
转载请明显位置注明出处:python – 将文档转换为pdf格式的有效方式