Creating PDF and Excel Reports in Odoo

Abid Patel
06-Nov-2024 Updated : 06-Nov-2024

Learn to create custom PDF and Excel reports in Odoo using QWeb templates and Python libraries. Enhance your reporting capabilities for better data insights.

Creating PDF and Excel Reports in Odoo

Generating reports is fundamental in Odoo. Reports are crucial for business insights. They might be for invoices, sales summaries or inventory sheets. They could be for any other data analysis. Odoo provides built-in back up for creating PDF reports. These reports are made with QWeb templates. Developers have the ability to generate Excel reports. They use Python libraries to do this task. A guide follows on how to establish both PDF and Excel reports in Odoo.

Why Generate PDF and Excel Reports you ask?

PDF Reports and Excel Reports serve distinct purposes in business operations. PDF reports are useful for generating formal documents. Documents like invoices or quotes. Such documents are easily shareable and readable. Excel Reports are ideal for detailed data analysis and manipulation. This is particularly true for larger datasets. Excel allows users to sort filter and calculate values directly.

Creating PDF Reports in Odoo

PDF reports in Odoo use QWeb. It is a templating engine. This engine is similar to HTML. What your task is you must define a custom layout. Here’s how to create a basic PDF report.

Step 1: Define the Report Action

Define the report action in your XML file. This is where you specify the model. Generate the report for a model. QWeb template to use. You should also specify this inside the report action.

xml

<report
    id="report_sale_order"
    model="sale.order"
    string="Sales Order Report"
    report_type="qweb-pdf"
    name="module_name.report_sale_order_template"
    file="module_name.report_sale_order_template"
    attachment_use="True"
/>

Step 2: Create the QWeb Template

Craft the template for report. Inside this template you have the potential to define the layout as well as the structure using XML tags.

xml

<template id="report_sale_order_template">
    <t t-call="web.html_container">
        <t t-foreach="docs" t-as="o">
            <div class="page">
                <h2>Sales Order: <t t-esc="o.name"/></h2>
                <p>Customer: <t t-esc="o.partner_id.name"/></p>
                <p>Date: <t t-esc="o.date_order"/></p>
                <!-- Add more fields as needed -->
            </div>
        </t>
    </t>
</template>

Step 3: Add to the Menu (Optional)

To offer access to report from user interface insert a menu item for it in these cases. There could be some instances where it is required.

xml

<menuitem id="menu_report_sale_order"
          name="Sales Order Report"
          parent="sales.menu_sale"
          action="report_sale_order"/>

Creating Excel Reports in Odoo

Excel reports get generated via Python libraries. These libraries include xlsxwriter openpyxl. It's essential to install the needed library if it's not present.

Step 1: Define Python Method for Excel Generation

Establish method in your model. It is for the management of Excel file creation. An example is provided using xlsxwriter.

python

from odoo import models
import xlsxwriter
import io
import base64

class SaleOrderReport(models.Model):
    _name = "sale.order.report"
    
    def generate_excel_report(self):
        output = io.BytesIO()
        workbook = xlsxwriter.Workbook(output)
        worksheet = workbook.add_worksheet('Sales Orders')
        
        # Define headers
        headers = ['Order Number', 'Customer', 'Total Amount']
        for col_num, header in enumerate(headers):
            worksheet.write(0, col_num, header)
        
        # Write data rows
        sale_orders = self.env['sale.order'].search([])
        for row_num, order in enumerate(sale_orders, start=1):
            worksheet.write(row_num, 0, order.name)
            worksheet.write(row_num, 1, order.partner_id.name)
            worksheet.write(row_num, 2, order.amount_total)
        
        workbook.close()
        output.seek(0)
        
        # Prepare file for download
        report_data = base64.b64encode(output.read())
        output.close()
        
        return {
            'type': 'ir.actions.act_url',
            'url': 'data:application/vnd.ms-excel;base64,' + report_data.decode(),
            'target': 'new',
            'download': True,
            'filename': 'SalesOrders.xlsx',
        }

Step 2: Render Report Downloadable

This code readies an Excel file. It has data from sale.order model. Further it encodes the file permitting browser download.

Conclusion

Odoo provides flexibility with PDF and Excel reports. This flexibility makes it powerful for generating custom reports. These reports are tailored to business needs. We can use QWeb for PDF templates. It offers a straightforward way to create visually appealing documents. Excel generation with Python is another aspect. It allows for more data manipulation. Both are excellent tools. They enhance reporting functionality in Odoo.

Make a Comment

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