按照文档转换API示例尝试使用Flask将msword文档转换为文本,但它不起作用。
这是代码
import os, json, requests
from flask import Flask, jsonify
from watson_developer_cloud import DocumentConversionV1
app = Flask(__name__) #create flask instance
@app.route('/')
def Welcome():
v = json.loads(os.getenv('VCAP_SERVICES'))
svcName = 'document_conversion'
svc = v[svcName][0]['credentials']
url = svc['url']
user = svc['username']
password = svc['password']
document_conversion = DocumentConversionV1(username=user, password=password,version='2015-12-15')
# Example of retrieving html or plain text
with open('./doc.docx', 'rb') as document:
config = {'conversion_target': DocumentConversionV1.NORMALIZED_TEXT}
print(json.dumps(document_conversion.convert_document(document=document, config=config),indent=2))
if __name__ == "__main__":
port = os.getenv('VCAP_APP_PORT', '5000')
app.run(host='0.0.0.0', port=int(port),debug=True)
这是运行时日志
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1836, in __call__
return self.wsgi_app(environ, start_response)
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1820, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1403, in handle_exception
reraise(exc_type, exc_value, tb)
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/_compat.py", line 33, in reraise
raise value
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/home/vcap/app/.heroku/python/lib/python3.5/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/vcap/app/welcome.py", line 39, in Welcome
password = svc['password']
document_conversion = DocumentConversionV1(username=user, password=password,version='2015-12-15')
# Example of retrieving html or plain text
with open('./doc.docx', 'rb') as document:
config = {'conversion_target': DocumentConversionV1.NORMALIZED_TEXT}
print(json.dumps(document_conversion.convert_document(document=document, config=config),indent=2))
if __name__ == "__main__":
port = os.getenv('VCAP_APP_PORT', '5000')
File "/home/vcap/app/.heroku/python/lib/python3.5/json/__init__.py", line 237, in dumps
**kw).encode(obj)
File "/home/vcap/app/.heroku/python/lib/python3.5/json/encoder.py", line 201, in encode
chunks = list(chunks)
File "/home/vcap/app/.heroku/python/lib/python3.5/json/encoder.py", line 436, in _iterencode
o = _default(o)
File "/home/vcap/app/.heroku/python/lib/python3.5/json/encoder.py", line 180, in default
raise TypeError(repr(o) + " is not JSON serializable")