How to upload and process the Excel file in Django

uploading and processing Excel file in Django

In this article we will discuss how to upload an Excel file and then process the content without storing file on server. One approach could be uploading the file, storing it in upload directory and then reading the file. Another approach could be uploading file and reading it directly from post data without storing it in memory and displaying the data.

We will work with the later approach here.

You may create a new project or work on existing code.

If you are setting up a new project then create a new virtual environment and install Django 2.0 and openpyxl modules in virtual environment using pip.

 

Assuming you are working on existing project, follow the below steps to upload and process the excel file in Django.

For this article, I have created a new small project using Django 2.0.3. Source code is available on Github. Please go through README.md  file to setup the project on your system.

We have an excel file user_data.xls  with below data in it.

uploading excel file in django

Uploading Excel file:

URLs:

Add a URL in urls.py  file of app.

In Django 2.0 it is mandatory to define the app_name  in urls.py  file if we are going to use namespace in project urlconf.

Views:

Create a function in views with the name index . This view will be responsible to read the excel file.

Here we are using openpyxl  module to read Excel file in Django.

First get the excel file from FILES in request and then get the desired worksheet from the workbook.

Now iterate over the rows in worksheet and for each row iterate over the cells and read the value in each cell.

We can get name of all sheets using below code.

 

You can either iterate over the sheet names or can get desired sheet by sheet name. To get the active sheet just use the wb.active .

You can get the value of any cell directly by using below method:

 

Index.html

Now create HTML form to upload the Excel file and to show its content. Use below code for the same.

Important: Do not forget to include enctype="multipart/form-data"  in form.

Other settings:

– Include the myapp urlconf in project’s urlconf file.

– Add the myapp  to the list of installed apps.

 

Now restart the python server and go to localhost:8000 . You will see below screen.

uploading and processing excel file in django

Browse the excel file user_data.xls  (available in source code on github) and upload it. After index view read the data from worksheet and render the page again, screen will look like below:

uploading and processing excel file in django

 

You can add validations to check file extension or file size. Refer the csv upload article for same.

After reading the content you can save it to Database or display it on html page.

In case of any issue comment below.

Host you Django App for free on PythonAnyWhere.

 

(Visited 214 times, 1 visits today)

You must read this :

Leave a Reply

Your email address will not be published. Required fields are marked *