gluc7
gluc7

Reputation: 575

Django- Create a downloadable excel file using pd.read_html & df.to_excel

I currently have a python script that uses pd.read_html to pull data from a site. I then use df.to_excel which sets 'xlsxwriter' as the engine.

I am trying to find a way to incorporate this into a django webapp. However, I am lost as how to do this or even know if possible.

I've seen a few ways to create downloadable excel files in django but none that have pandas as the driving force of creating the data in the excel file. My python code for creating the excel file without django is somewhat long so not sure what to show. Below is part of my pandas code:

        xlWriter = pd.ExcelWriter(excel_sheet2, engine='xlsxwriter')
        workbook = xlWriter.book

        money_fmt = workbook.add_format({'num_format': 42, 'align': 'center', 'text_wrap': True})
        text_fmt = workbook.add_format({'bold': True, 'align': 'center', 'text_wrap': True})



        for i, df in enumerate(dfs):
            for col in df.columns[1:]:
                df.loc[df[col] == '-', col] = 0 
                df[col] = df[col].astype(float)

            df.to_excel(xlWriter, sheet_name='Sheet{}'.format(i))

Below is my templates.html code

{% block content %}
<form type="get" action="." style="margin: 0">
 <input id="search_box" type="text" name="search_box" placeholder="Enter URL..." >
 <button id="search_submit" type="submit" >Submit</button>
</form>
{% endblock %}

And this is the beginning of my views.py

def financials(request):
    return render(request, 'personal/financials.html')

    if request.method == 'GET':
        search_query = request.GET.get('search_box', None)
        url = search_query

        dfs = pd.read_html(url, flavor='html5lib')

Upvotes: 1

Views: 2085

Answers (2)

gluc7
gluc7

Reputation: 575

I just wanted to add what I finally came up with that got everything working. Instead of including my data within the HttpResponse, I included the response within the wb.save() command. This got everything working correctly including my formatting of the spreadsheet prior to downloading.

wb = load_workbook(excel_sheet2)

response = HttpResponse(content_type='application/vnd.ms-excel')
response['Content-Disposition'] = 'attachment; filename= "Data.xlsx"'

wb.save(response)

return response

Upvotes: 0

zunayed ali
zunayed ali

Reputation: 46

Why don't you just call your pandas functions within the Django view and save the file to /tmp. Once you have the file you can just send it and tell the browser to treat it as a file in your response.

You can then just return the file

from django.http import HttpResponse

def my_view(request):
    # your pandas code here to grab the data
    response = HttpResponse(my_data, content_type='application/vnd.ms-excel')
    response['Content-Disposition'] = 'attachment; filename="foo.xls"'
    return response

https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

Upvotes: 1

Related Questions