3

I am learning django framework and trying to read an excel file

I want to upload file using html form(POST Method) and want to read in python for calculations so what should I do really confused ? Code Is Here form.html

<form method="POST" action="minmax_ans.html">{% csrf_tocken %}
   {{form.as_p}}
   <p><b><h2>Upload File</h2></b><p>
   <input type="file" name="file" value="{{ source.title }}">
   <input type="submit">
</form>

views.py

def upload_file(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            handle_uploaded_file(request.FILES['file'])
            return HttpResponseRedirect('/success/url/')
    else:
        form = UploadFileForm()
    return render(request, 'upload.html', {'form': form})

I want to do some calculations using data in the file uploaded through html form if it is an excel file and display the result on result.html which I have not created yet because I cannot read the file

1

2 Answers 2

1

You can do something like this

import pandas as pd
def handle_uploaded_file(f):
    pd.read_excel(f, index_col=0)

Pandas read_excel accepts argument io in str, ExcelFile, xlrd.Book, path object or file-like object.

Sign up to request clarification or add additional context in comments.

Comments

0

Probably I would start by reading the Django documentation:
https://docs.djangoproject.com/en/1.10/topics/http/file-uploads/

To read the excel file you can use pandas:
http://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_excel.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.