Odoo Help


Reportlab: Conditional Table Header

Sergii Kauk
on 5/6/13, 12:30 PM 3,601 views

I'm trying to make a conditional table header in reports which shows Discount column only when there is a discount. The condition works fine but I have an issue with formatting. Since blockTable colWidths parameter should correspond to the number of columns, I get an error when one of the columns gets deleted.

So far I came up with this markup as a test case:

<docAssign var='d' expr='0'/>

  <para>[[ line.discount > 0 or removeParentNode('para') ]] <docExec stmt='d+=1'/></para>

<docIf cond='d>0'>
  <para style="Standard">There are discounts</para>
    <para style="Standard">No discounts</para>

<docPara expr='d' format='The number of discounts is %(__expr__)d' />

However, I don't see any output from docIf condition and docPara. Moreover, examples of conditional format operators from RML User Guide doesn't produce any output either. Does someone have any idea of how to work around that?





| 5 3 6
Jakarta, Indonesia
On 7/16/13, 8:09 AM

AFAIK, conditional formatting is not implemented in the OpenERP's RML processing. A way to work around it is to use [[ ]] block, such as:

[[ line.discount > 0 and 'There are discounts' or 'No discounts' ]]

If this is supposed to be based on total discount, then you need to create the function to calculate/check the total discount.

The similar approach goes for the "The number of discounts is XXXX" line:

     The number of discounts is [[ getDiscountedLine() and getDiscountedLine() or removeParentNode('para') ]]

Where getDiscountedLine() method need to be implemented in the python code (the same method can be used for the logic above)


Brett Lehrer

--Brett Lehrer--

| 5 5 8
Milwaukee, United States
--Brett Lehrer--
Director of IT and open source enthusiast.
Brett Lehrer
On 8/12/15, 1:24 PM

Ivan's answer would be the correct way to approach this particular issue.

However, I recently needed to implement the docIf RML flowable (in OpenERP 7.0) to guarantee that invoices generated would always be an even number of pages so they could be easily printed double-sided.  I figured I'd share the implementation, as there is very little explanation elsewhere on how to do this.

Here's the RML of the bottom of the invoice:

    <docIf cond="doc.page%2==1">

To make OpenERP properly parse the docIf flowable, you need to edit the following file:


Before line 888 (the else block reporting that the flowable is not implemented), add the following lines:

 elif node.tag == 'docIf':
    return platypus.flowables.DocIf(node.get('cond'), self.render(node))

For reference, the surrounding code in trml2pdf.py should now look something like this (new lines in bold):

elif node.tag == 'frameEnd':
    return EndFrameFlowable()
elif node.tag == 'hr':
    width_hr=node.get('width') or '100%'
    color_hr=node.get('color')  or 'black'
    thickness_hr=node.get('thickness') or 1
    lineCap_hr=node.get('lineCap') or 'round'
    return platypus.flowables.HRFlowable(width=width_hr,color=color.get(color_hr),thickness=float(thickness_hr),lineCap=str(lineCap_hr))
elif node.tag == 'docIf':
    return platypus.flowables.DocIf(node.get('cond'), self.render(node))
    sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (node.tag,))
    return None

The end result is that you can now utilize RML docIf statements in templates, and in my example, all invoices printed will always be an even number of pages.

About This Community

This platform is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.


Odoo Training Center

Access to our E-learning platform and experience all Odoo Apps through learning videos, exercises and Quizz.

Test it now

Question tools

0 follower(s)


Asked: 5/6/13, 12:30 PM
Seen: 3601 times
Last updated: 8/12/15, 1:24 PM