最简单的uWSGI python应用程序由一个带有两个参数的应用程序函数environ和start_response组成
import os #
import pylokit #
import tempfile #
import webob #
office = pylokit.Office('/usr/lib/libreoffice/program') # LibreOffice
def application(environ, start_response): # uWSGI
request = webob.Request(environ) #
file = request.POST['file'] # multipart/form-data file
filename, extension = os.path.splitext(file.filename) #
with tempfile.NamedTemporaryFile(suffix=extension) as inp, tempfile.NamedTemporaryFile(suffix='.%s' % request.path.split('/')[-1]) as out: # ( unoconv-api)
inp.write(file.file.read()) #
inp.flush() # (.. LibreOfficeKit - )
with office.documentLoad(inp.name) as doc: #
doc.saveAs(out.name) # ( )
with open(out.name, 'rb') as out2: #
response = webob.Response(body=out2.read()) #
return response(environ, start_response) #
当然,您还可以添加任何错误的处理。
将测试一页odt文件转换为pdf的速度比unoconv-api快1.5倍。