Skip to content
Advertisement

Reading a docx file from s3 bucket with flask results in an AttributeError

I got so many different errors, I don’t even know which is pertinent to mention but it’s not about the credentials because I can upload files already and I can read a txt file. Now I want to read a docx.

I created a form in my index.html with just a text area to write the exact name of the file and a submit input that will open a new window with the number of paragraphs from my docx file in my AWS S3 bucket.

The error I’m getting is this:

AttributeError: ‘StreamingBody’ object has no attribute ‘seek’

My code looks like this:

JavaScript

Advertisement

Answer

I checked out the documentation for python-docx, specifically the Document-constructor:

docx.Document(docx=None)

Return a Document object loaded from docx, where docx can be either a path to a .docx file (a string) or a file-like object. If docx is missing or None, the built-in default document “template” is loaded.

It seems to expect a file-like object or the path to a file. We can turn the different representations we get from boto3 into a file-like object, here’s some sample code:

JavaScript

This is what it looks like:

JavaScript
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement