Closed laurent-brisbois closed 8 months ago
A few questions:
PK
which indicates that it is a zip/xlsx file which is correct.) Hello !
Thank you for your first answer.
I removed authentication_classes
and permission_classes
on purpose here.
from io import BytesIO
import xlsxwriter
from django.http import HttpResponse # Or use rest_framework.response.Response
from rest_framework.decorators import api_view
from rest_framework.request import Request
@api_view(http_method_names=["GET"])
def export_xlsx(request: Request):
output = BytesIO()
workbook = xlsxwriter.Workbook(output, {"in_memory": True})
data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
for row_num, columns in enumerate(data):
for col_num, cell_data in enumerate(columns):
worksheet.write(row_num, col_num, cell_data)
workbook.close()
output.seek(0)
response = HttpResponse(
content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
)
response["Content-Disposition"] = "attachment; filename=othertest2.xlsx"
return response
api_view
from rest_framework
.Note that I also tried to import the file in Google Sheet and it loads forever, I guess it cannot open it either.
Thank you for your help !
For item 2 could you run the doc example, unedited, from the command line and let me know if it works.
I installed xlsxwriter on my local machine (Ubuntu 22.04) widely like so :
$ sudo apt install python3-xlsxwriter
Not sure it is the 3.2.0 version though.. Seems like it is 3.0.2
then in python3 directly in my terminal (it worked) :
My system python is 3.11.6 whereas in my docker container it is 3.8.13 but I don't think it's the problem here.
Also I tried to run the exact same example for Django as in the docs : but still the same :
Also I tried to run the exact same example for Django as in the docs : but still the same :
Could you attach the output file django_simple.xlsx
to this GitHub issue.
Could you attach the output file django_simple.xlsx to this GitHub issue.
Sure, here it is :
Okay I found the issue and I have to say... my bad on this.
I use Axios for making queries between my vuejs frontend and my django backend.
And whereas I already did file download in the past, I was considering it should work and that the issue couldn't come from there.
But I completely forgot to set my responseType
as blob
for this new API service call in my frontend...
Thank you anyway for your reactivity, answers, and your willingness to help :ok_hand: :pray: .
Maybe it is worth to be mentioned though in examples that show API endpoints.
But I completely forgot to set my
responseType
asblob
for this new API service call in my frontend...
No worries. I was zeroing in on the fact that the binary file had been treated as a text file someplace and "\n"
had been replaced with "\r\n"
or similar.
I will close it. Thanks for the followup.
Current behavior
Simple example, I try to generate the file with example first to see if it works but it doesn't, see below :
Expected behavior
I'd like to be able to at least generate the sample from the docs.
Sample code to reproduce
Environment
Any other information
I don't think it's a matter of returning the file correctly because I'm able to download images or CSV files with another endpoints. This is from a personal project and given example doesn't work. In another project I used once, there was
xlsxwriter==1.4.3
with lots of formattings and so on and it works on the same computer.OpenOffice and LibreOffice users