Search code examples
pythoncolorsreportlab

Defining our own colors (RGB ?) for Intra-paragraph markup color in reportlab


For generating PDF files with reportlab / python, it is possible to define very easily the color of a text (and lot other things) by using the so called "Paragraph XML Markup Tags". For example a green text:

rapport.append(Paragraph('<font size = 14 color = "green" > <b> Toto is a naugthy boy </b></font>', styles['Left']))

But, is it possible to define our own color (for example by using RGB code) ?


Solution

  • It is actually really straightforward, you can just replace green with any hex RGB color like #424242. So in your example it will look like this:

    rapport.append(Paragraph('<font size=14 color="#424242"><b>Toto is a naugthy boy</b></font>', styles['Left']))
    

    But it is also possible to use most of the HTML colors like:

    rapport.append(Paragraph('<font size=14 color="rgb(191, 255, 0)"><b>Toto is a naugthy boy</b></font>', styles['Left']))
    rapport.append(Paragraph('<font size=14 color="hsl(75, 100%, 50%)"><b>Toto is a naugthy boy</b></font>', styles['Left']))