Django Create Word Document File Example

Published On: 04/08/2022 | Category: Django

Hi Guys,

In this tutorial we will go over the demonstration of django create word document file. you will learn generate the ms word document in django. if you have question about how to generate word document from html in django? then I will give simple example with solution. if you have question about django download docx file then I will give simple example with solution. You just need to some step to done django create word document file example.

The Django exporting files is a frequently happen feature where the user could get their data out. As the backend side, my Django app could help me export a docx file by using a library named python-docx.

You can use these examples with django3 (django 3) version.

let's see below simple example with output:

Step 1 : Create a Project

In this step, we’ll create a new django project using the django-admin. Head back to your command-line interface and run the following command:

django-admin startproject example
Step 2 : Create a App
python3 startapp core
Step 3 : Update

Here, do not forget to register the new app in the file. Under installed apps, just add ‘core’ to the list:

Step 4 : Install python-docx

Next step, we will require install the python-docx library following below command:

pip install python-docx
Step 5 : Creating the Views

In this step, we need to create the views for performing ajax image upload to the database.Open the core/ file and add.

from django.http import HttpResponse
from docx import *
from docx.shared import Inches
from datetime import date
from io import BytesIO

def TestDocument(request):

    document = Document()
    # ---- Cover Letter ----
    document.add_paragraph("%s" %'%B %d, %Y'))

    document.add_paragraph('Welcome to!')
    document.add_paragraph('Tuts-Station Blog provides you latest Code Tutorials on PHP, Laravel, Codeigniter, JQuery, Node js, React js, Vue js, PHP, and Javascript. Mobile technologies like Android, React Native, Ionic etc.')

    document.add_paragraph('Thank You!!,')

    # Prepare document for download        
    # -----------------------------
    f = BytesIO()
    length = f.tell()
    response = HttpResponse(
    response['Content-Disposition'] = 'attachment; filename=' + docx_title
    response['Content-Length'] = length
    return response
Step 6 : Creating URLs

In this section, we’ll create the urls to access our views.Go to the core/ file and update it as follows:

from django.urls import path
from . import views

urlpatterns = [
    path('docs/', views.TestDocument, name='download')

Next, we will require the modify the your root preoject folder lets update the file.

from django.contrib import admin
from django.urls import path, include

urlpatterns = [
    path('', include('core.urls'),
Run the Server

In this step, we’ll run the local development server for playing with our app without deploying it to the web.

python runserver

Next, go to the address with a web browser.

Preview Doc:

I Hope It will help you....