How to export excel files in a python django application

11
How to export Excel files in a Python/Django application ASSIST Software Romania- Popovici Irina Django

Transcript of How to export excel files in a python django application

Page 1: How to export excel files in a python django application

How to export Excel files in a Python/Django application

ASSIST Software Romania-

Popovici Irina

Django

Page 2: How to export excel files in a python django application

Introduction

When creating a web management application, many clients require pages which display statistics. Usually this feature comes along with the option to download the data in an Excel and/or PDF format. Therefore, this article will show you how to create an Excel document and make it available for download in a Python/Django application.

Page 3: How to export excel files in a python django application

Creating a working environment

First of all, when working with Django applications it is advised to create an environment (with virualenv)and install all the modules in that environment. This practice is very useful when you’re working on different websites that don’t use the same versions.

Speaking of modules, in this example we will be using:•Python 2.7•Django 1.8.2•XlsxWriter 0.7.3

You can download the github demo project by clicking here.

Page 4: How to export excel files in a python django application

How to serve a file for download

To begin with, in the HTML file there is a form that contains the “Excel Report” button. Therefore, in the corresponding Django view we receive the request to download ‘excel’. Thus, we have to send the file to the user and we can do this through the following code. The result is that the user receives a file named “Report.xlsx”:

Page 5: How to export excel files in a python django application

How to create an Excel file

• The following code will create a blank Excel file.

Page 6: How to export excel files in a python django application

How to resize rows and columns

1. The columns can have a constant width. Suitable examples for this case are Date, Temperature, Wind and Precipitation columns. Thus, the code from below displays the change for Wind column. This line can be added after or even before the loop that adds the data.

Page 7: How to export excel files in a python django application

How to resize rows and columns

The columns can adapt their width according to the data that they contain, in this instance: Town and Description. These values have a maximum length constraint in the database. Therefore, we can set a column width according to the biggest length of all the data:

Page 8: How to export excel files in a python django application

How to resize rows and columns

The column can have a constant width, but the row height can vary. The last column, Observations, is where we can apply this condition. In this exercise, the width will be 25, meaning that we will consider that one row cannot have more than 25 characters:

Page 9: How to export excel files in a python django application

How to add charts

• Read how to add charts to your excel in this article - http://assist-software.net/blog/how-export-excel-files-pythondjango-application

Page 10: How to export excel files in a python django application

Other topics drawn in article

• How to add title and headers• How to write data• How to add formulas• Line charts• Column charts• Pie charts

Page 11: How to export excel files in a python django application

Links

• You can read whole article, see the entire code and download the project from github by accessing the link with the article.

• http://assist-software.net/blog/how-export-excel-files-pythondjango-application