Skip to content
Advertisement

How to open and process .rtf files in Flask

I am trying to send a request including a rtf file and process it in Flask.

In my python script, I used the striprtf lib to read this file and then process it.

rtf = Path(file_path).read_text()
text = rtf_to_text(rtf)

Now I want to wrap this script into flask. I got error said: TypeError: expected string or bytes-like object. How can I read this rtf file in the flask?

file = request.files['file']

text = rtf_to_text(file)

Advertisement

Answer

Try the read method of file, which is a werkzeug.datastructures.FileStorage object.

You’ll then need to decode this to provide rtf_to_text with the string it expects.

file = request.files['file']
as_string = file.read().decode('utf-8')
text = rtf_to_text(as_string)
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement