0

I'm trying to send an excel file back to the user's browser in a zipped folder.

I build the excel file(s) using openpyxl , and here is my code :

@app.route('/test', methods=['GET', 'POST'])
    def test():

        # ...

        from zipfile import ZipFile

        # ...        

        buf = BytesIO()
        wb.save(buf)
        buf.seek(0)

        with ZipFile(buf, 'w') as myzip:
            myzip.write(buf, arcname='test.xlsx')

        return send_file(myzip, attachment_filename='folder.zip', as_attachment=True)

However, i get the following error:

TypeError: stat: path should be string, bytes, os.PathLike or integer, not _io.BytesIO

How can i do this without using a bytesio object? or can i modify the code so it can accommodate a bytesio object?

many thanks in advance.

anehme
  • 536
  • 1
  • 6
  • 18
pablowilks2
  • 299
  • 5
  • 15

1 Answers1

0

Try this code.

@app.route('/test', methods=['GET', 'POST'])
    def test():

        # ...

        from zipfile import ZipFile

        # ...        

        buf = BytesIO()
        wb.save(buf)
        buf.seek(0)

        filename = 'folder.zip' # assuing zip file u need
        with ZipFile(filename, 'w') as myzip:
            myzip.writestr(zinfo_or_arcname='test.xlsx', bytes=buf.read())

        return send_file(myzip, attachment_filename='folder.zip', as_attachment=True)

Changes, using writestr instead of write

        filename = 'folder.zip' # assuing zip file u need
        with ZipFile(filename, 'w') as myzip:
            myzip.writestr(zinfo_or_arcname='test.xlsx', bytes=buf.read())
arunp9294
  • 767
  • 5
  • 15