following the document conversion API example trying to use Flask to convert msword document to text, but it does not work.
Here is the code
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)
Here is the runtime log
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")
The documentation appears to be out of date. The object returned is a Response object, as seen in the Requests library. In my experience, you can just use .text to get a string representing the JSON. I should say, you might have to use the .json representation to get something you can call .dumps() on.