Download generated excel file using xslxwriter on Python(Django) - python

I am working with Python using the Django framework, at this moment I am generating some reports in Excel, for that I use the xslxwriter library, the user tries to download the file from the platform, I do not get the download and instead it The answer is strange characters, I think that's the Excel file, anyway I don't know how to make that file download.
This snippet is the one that is supposed to download the file
workbook.close()
output.seek(0)
response = HttpResponse(output.read(), content_type="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet")
response['Content-Disposition'] = "attachment; filename=test.xlsx"
output.close()
return response
This is the response I get from the frontend
Thank you very much in advance.

Related

Create a pdf with Python Flask from file

I need to write a python flask service which takes in input a file (.doc or .ppt) and returns the same file converted in pdf without saving it in a folder. This service has to run on a server.
I tried with pdfkit but I've it not easy to install it on a server.
What i need is something like this:
def post(self):
file = request.files['file_request']
pdf = pdfkit.from_file(file, False) #or any other library to convert the file
return send_file(
pdf,
mimetype='application/application/pdf',
attachment_filename="File.pdf",
as_attachment=True,
cache_timeout=0
)
Is it possible to do it? Could somebody help me?
Thanks

Post pdf to api (python) returns response with wrong encoding

I am trying to use an ocr API with python to convert pdf to text. The API i'm using is : https://www.convertapi.com/pdf-to-txt . When i upload the file through the website it works perfectly but the API call has the following issue:
Python code:
import requests
url ='https://v2.convertapi.com/convert/pdf/to/txt?Secret=mykey'
files = {'file': open('C:\<some_url>\filename.pdf', 'rb')}
r = requests.post(url, files=files)
The API call works fine, but it when i try to access the response through
r.text
it returns giberish: (Notice the FileData section)
'{"ConversionCost":4,"Files":[{"FileName":"stateoftheartKWextraction.txt","FileExt":"txt","FileSize":60179,"FileData":"QXV0b21hdGljIEtleXBocmFzZSBFeHRyYWN0aW9uOiBBIFN1cnZleSBvZiB0aGUgU3RhdGUgb2YgdGhlIEFydA0KDQpLYXppIFNhaWR1bCBIYXNhbiAgYW5kICBWaW5jZW50IE5nDQpIdW1hbiBMYW5ndWFnZSBUZWNobm9sb2d5IFJlc2VhcmNoIEluc3RpdHV0ZSBVbml2ZXJzaXR5IG9mIFRleGFzIGF0IERhbGxhcyBSaWNoYXJkc29uLCBUWCA3NTA4My0wNjg4DQp7c2FpZHVsLHZpbmNlfUBobHQudXRkYWxsYXMuZW...
Even if i use json load to convert it into a dict, it still prints the text in giberish.
I've tried to upload the file as not binary but that doesn't work(it throws an exception).
I've tried many pdf files and they all were in english.
Thank you.
The text is decoded, so you need to decode it. Let's take the first file as an example.
import base64
r = r.json()
text = r['Files'][0]['FileData']
print(base64.b64decode(text))
By the way, they seem to have a Python library as well, you might want to check that out: https://github.com/ConvertAPI/convertapi-python

What response should I return when returning an excel document in Django REST framework

I use openpyxl create a workbook, and return a Response like this:
from openpyxl import Workbook
wb = Workbook()
response = HttpResponse(save_virtual_workbook(wb), content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename="foo.xlsx"'
return response
but frontend coder said he accept gibberish, you should return a bytearray like 0101010101,he can analysis and download
Now I return a response like this, he use node.js not vue download through a complex process
I am confused, what should I do?
To make a direct link to file try to use application/vnd.openxmlformats-officedocument.spreadsheetml.sheet content type.
I recomend you to use django-excel to work with excel. It's pretty easy to use and you could integrate different formats later.
There're alot of examples of downloading views. Check the methods starts with make_response_from_. I guess you could use some of them to build a file and return it as response.

Flask: force download pdf files to open in browser

I'm trying to download a PDF file using flask but I don't want the file to download as an attachment. I simply want it to appear in the user's browser as a separate webpage. I've tried passing the as_attachment=False option to the send_from_directory method but no luck.
Here is my function so far:
#app.route('/download_to_browser')
def download_to_browser(filename):
return send_from_directory(directory=some_directory,
filename=filename,
as_attachment=False)
The function works in the sense that the file is downloading to my computer but I'd much rather just display it in the browser (and let the user download the file if they want to).
I read here I need to change thecontent-disposition parameter but I'm not sure how that can be done efficiently (perhaps using a custom response?). Any help?
Note: I'm not using Flask-Uploads at the moment but I probably will down the line.
You can try to add the mimetype parameter to send_from_directory:
return send_from_directory(directory=some_directory,
filename=filename,
mimetype='application/pdf')
That works for me, at least with Firefox.
If you need more control over headers, you can use a custom response, but you will lose the advantages of send_file() (I think it does something clever to serve the file directly from the webserver.)
with open(filepath) as f:
file_content = f.read()
response = make_response(file_content, 200)
response.headers['Content-type'] = 'application/pdf'
response.headers['Content-disposition'] = ...
return response

Django: MP3 file being opened, not saved, on Apple devices

I am working with Python and Django 1.9.
I manage to serve an MP3 file for download and it works fine on my computer and on my Android phone. However, on my iPad, the file isn't downloaded and saved as a file; it is directly opened in both the Safari and Chrome browsers.
Is there any way to save the file on the iPad?
This is the code snippet that I'm using:
fsock = open(filename, 'r')
response = HttpResponse(fsock, content_type='audio/mpeg')
response['Content-Disposition'] = "attachment; filename=%s.mp3" % (filename)
return response
Is there something I should change in my code or is it rather a constraint from Apple's OS to avoid saving downloaded MP3 files directly from the browser?

Categories

Resources