简体   繁体   English

Django - 使用 pd.read_html & df.to_excel 创建可下载的 excel 文件

[英]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.我目前有一个 python 脚本,它使用 pd.read_html 从站点中提取数据。 I then use df.to_excel which sets 'xlsxwriter' as the engine.然后我使用 df.to_excel 将“xlsxwriter”设置为引擎。

I am trying to find a way to incorporate this into a django webapp.我正在尝试找到一种方法将其合并到 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.我已经看到了几种在 django 中创建可下载 excel 文件的方法,但没有一种方法以 Pandas 作为在 excel 文件中创建数据的驱动力。 My python code for creating the excel file without django is somewhat long so not sure what to show.我用于在没有 django 的情况下创建 excel 文件的 python 代码有点长,所以不确定要显示什么。 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下面是我的 templates.html 代码

{% 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这是我的 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')

Why don't you just call your pandas functions within the Django view and save the file to /tmp .为什么不直接在 Django 视图中调用 pandas 函数并将文件保存到/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 https://docs.djangoproject.com/en/dev/ref/request-response/#telling-the-browser-to-treat-the-response-as-a-file-attachment

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.我没有在 HttpResponse 中包含我的数据,而是在 wb.save() 命令中包含了响应。 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM