我一直在尝试找到将文档(例如 doc、docx、ppt、pptx)转换为 pdf 的有效方法。到目前为止,我已经尝试过docsplit和oowriter
,但两者都花费了 10 秒以上的时间来完成大小为 1.7MB的pptx 文件的工作。有人可以建议我更好的方法或建议来改进我的方法吗?
我试过的:
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 秒