Enable Dark Mode!
how-to-generate-microsoft-word-documents-in-a-zip-in-odoo-15.jpg
By: Vishnu Vijayan

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

Reports are crucial to the operation of any business since they provide information about the operation's performance and reflection, as well as the success and failure rates of the operation. Odoo provides a platform for creating reports and analyzing business trends.

By default, Odoo supports PDF reports, enabling you to produce PDF reports for custom modules as well. Additionally, we can produce excel reports in Odoo that will be very beneficial for your company's business leaders. At times, we can also require reports in another format, such as Microsoft Word (.docx)

This blog will discuss how to download multiple documents in a zip file and generate a Microsoft Word (.docx) report.

We need to install the Python library python-docx in order to generate the Microsoft Word (.docx) report. By using the Python-docx library, we can create and update Word files, such as Microsoft Word (.docx) files.

By using the following line of code, you can install it easily,

pip install python-docx

Install python-docx in the terminal.

Now let's discuss how to generate Microsoft Word files (.docx) from a zip file in Odoo 15. The following code shows how to implement the model 'test.doc' in Python

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,
               }

For the print button, we have a method that returns the URL /web/binary/download_docx_report. In addition, we will need to define a controller for it

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')])

Taking a closer look at each element of the above code:

stream = io.BytesIO() 

// The memory buffer is used to keep data as bytes.

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

// Create zip

 document1 = Document()

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

//The document can be added with a paragraph by adding an element add_paragraph, where the paragraph to be displayed can be added.

document1_file_name = "test1.docx"

// The first step is to set the name of the document, here I have given it the name test1.docx, but if the name has to be generated based on the document, you can also do it from the other methods. 

 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 that you can loop as per the customer's requirement.

zip_archive.close()

// Close the zip

bytes_of_zipfile = stream.getvalue()

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')])

// Respond to the request with the appropriate response. Use the method make_response to create a response using the ZIP file you created.

Let's see how it works. When you click on the print button, the controllers return the response with the documents in a ZIP file.

The downloaded ZIP file is stored in the system's local storage.

how-to-generate-microsoft-word-documents-in-a-zip-in-odoo-15-1

In this way, we can download multiple documents in a zip file and generate a Microsoft Word (.docx) report.


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



0
Comments



Leave a comment



whatsapp_icon
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