1

I am using FastAPI to retrieve the docx file from the frontend as bytes file and to save it on the backend's filesystem. For this purpose I installed the python-docx library. However, when I am trying to run the following code, just like it is said in python-docx docs (https://python-docx.readthedocs.io/en/latest/user/documents.html#opening-a-file-like-document)

async def create_file(textfile: UploadFile, user):
    path = os.path.abspath(textfile.filename)
    f = open(textfile.filename, 'rb')
    document = Document(f)
    f.close()

It throws an error: zipfile.BadZipFile: File is not a zip file

I tried to use the following solution (https://stackoverflow.com/a/11385480/21602190) but as I use bytes, it is pointless.

  • Can you open the docx file in Word? It looks like you might have a corrupt file. The error may be in that code rather than the opening code. – Toby Allen Apr 16 '23 at 20:21
  • Yes, I can. One important thing to note - my backend is dockerized, maybe that can provoke the issue. – HeteroChromia Apr 16 '23 at 20:25
  • I managed to save it eventually, but it saves weird gibberish into the file instead of actual text, how can I decode it? – HeteroChromia Apr 17 '23 at 16:09

0 Answers0