Enable Dark Mode!
generate-microsoft-word-documents-within-zip-odoo-14.jpg
By: Nimisha Muralidhar

How To Generate Microsoft Word Documents in a Zip In Odoo 14?

Odoo 14

Reports play an important role in the business as it helps to know the performance and reflection of the business and also we can check the success and failure rates of the business from the reports. Odoo offers a platform to generate reports and analyze business developments.

Odoo supports PDF reports by default allowing you to create PDF reports for the custom modules as well. Additionally, we can generate excel reports in Odoo which will be extremely helpful for the business executives of your company. At certain times we may also need the reports in the other format as well such as Microsoft Word (.docx) 

In this blog, we are going to discuss how to generate a Microsoft Word (.docx) report in a custom module and to download multiple documents in a single zip.

To generate the Microsoft Word (.docx)  report we need to install a python library python-docx. We use the python-docx library to create and update word files i.e. Microsoft Word (.docx) files.

Installation is simple, you can install it by using the following line of code,

pip install python-docx

Open the terminal and install python-docx.

Now we will move on to discuss how to generate Microsoft Word (.docx) within a zip in odoo 14. The below code shows the python file for the model ‘test.doc’ to be implemented

from odoo import models, fields
class TestDoc(models.Model):
   _name = 'test.doc'
   name = fields.Char(String='Name', help="Name of the customer", required=True)
   
   def print_pdf(self):
       return {'type': 'ir.actions.act_url',
               'url': '/web/binary/download_docx_report',
               'target': 'self',
               'res_id': self.id,
               }

Here we have a method for the print button that returns to the URL- /web/binary/download_docx_report. Moreover, we will have to define a controller for it. The view for the model test.doc is shown below image,

generate-microsoft-word-documents-within-zip-odoo-14-1

Controller for the route /web/binary/download_docx_report is defined as follows

from odoo import http
from odoo.http import request
from docx import Document
import zipfile
import io
class Binary(http.Controller):
   @http.route('/web/binary/download_docx_report', type='http', auth="public")
   def download_function_descriptions(self):
       stream = io.BytesIO()
       zip_archive = zipfile.ZipFile(stream, 'w', compression=zipfile.ZIP_DEFLATED)
       document1 = Document()
       document1.add_paragraph('Test Paragraph for FIRST document')
       document1_file_name = "test1.docx"
       document1.save(document1_file_name)
       zip_archive.write(document1_file_name)
       document2 = Document()
       document2.add_paragraph('Test Paragraph for SECOND document')
       document2_file_name = "test2.docx"
       document2.save(document2_file_name)
       zip_archive.write(document2_file_name)
       zip_archive.close()
       bytes_of_zipfile = stream.getvalue()
       return request.make_response(bytes_of_zipfile,[('Content-Type', 'application/zip'),('Content-Disposition', 'attachment')])

Let's look at each element of the above code in detail:

stream = io.BytesIO() 

//To keep data as bytes in the memory buffer.

zip_archive = zipfile.ZipFile(stream, 'w', compression=zipfile.ZIP_DEFLATED)

 // Create zip 

 document1 = Document()

 // Open document i.e. for first document. Here we have created multiple documents and downloaded it as a ZIP file, hence the name document1.

    document1.add_paragraph('Test Paragraph for FIRST document') 

// To add a paragraph in the document we have to define an element add_paragraph, where you can add the paragraph which has to be displayed in the document. We have other elements to add table, header and many other aspects to the document. To know more, please go through the python-docx documentation.

    document1_file_name = "test1.docx"

//  Set a name for the document, here as an example I have given the name as test1.docx, When you have multiple docx to be generated and if the name has to be generated based on the document, you can also generate the name from the other methods as well. 

    document1.save(document1_file_name)

// Save the created document in the file name test1.docx 

    zip_archive.write(document1_file_name)

// Created document is added to the generated ZIP file.

    document2 = Document()

    document2.add_paragraph('Test Paragraph for SECOND document')

    document2_file_name = "test2.docx"

    document2.save(document2_file_name)

    zip_archive.write(document2_file_name)

Similarly, the process is repeated to generate a second document which you can loop as per the customer requirement.

    zip_archive.close()

// Close the zip

    bytes_of_zipfile = stream.getvalue()

Now in the stream, we have the data as bytes, to get the value from the stream we use the element getvalue()

    	return request.make_response(bytes_of_zipfile,[('Content-Type', 'application/zip'),
                                  ('Content-Disposition', 'attachment')])

// Return the request with a respective response. To make the response with the created ZIP file use the method make_response.

Let us check how it works. When you click on the print button it redirects to the corresponding URL that is being defined and hence the controllers return the response with the documents in a ZIP file.

generate-microsoft-word-documents-within-zip-odoo-14-1

Upon choosing the Document to be printed a pop up window will be depicted as shown in the following image. You can choose the Save File option for the Zip file to be downloaded.

generate-microsoft-word-documents-within-zip-odoo-14-1

The downloaded ZIP file is depicted in the local storage of the system.

generate-microsoft-word-documents-within-zip-odoo-14-1

You can extract the ZIP file to obtain the respective field embedded in the ZIP using an external ZIP Extractor tool.

generate-microsoft-word-documents-within-zip-odoo-14-1

The generated report will be depicted upon the extraction of the ZIP file.

generate-microsoft-word-documents-within-zip-odoo-14-1Upon opening the extracted folder, you could find the generated documents.

Open each of the documents or the corresponding file to view the contents.

generate-microsoft-word-documents-within-zip-odoo-14-1

Document - 1

generate-microsoft-word-documents-within-zip-odoo-14-1

Document - 2

In this manner, you can generate documents within a ZIP file in Odoo 14 which can be further extracted to local storage.


If you need any assistance in odoo, we are online, please chat with us.



0
Comments



Leave a comment



whatsapp
location

Calicut

Cybrosys Technologies Pvt. Ltd.
Neospace, Kinfra Techno Park
Kakkancherry, Calicut
Kerala, India - 673635

location

Kochi

Cybrosys Technologies Pvt. Ltd.
1st Floor, Thapasya Building,
Infopark, Kakkanad,
Kochi, India - 682030.

location

Bangalore

Cybrosys Techno Solutions
The Estate, 8th Floor,
Dickenson Road,
Bangalore, India - 560042

Send Us A Message