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')