Text color by color names using `webcolors` module

For a Python module that can change color names to RGB triplets see webcolors

`webcolors` can be installed by issuing the command:

pip install webcolors

The following code uses the module to facilitate declaring colors by their name, like ‘navy’.

# Import FPDF class
from fpdf import FPDF
from webcolors.webcolors import name_to_rgb

# Create instance of FPDF class
# Letter size paper, use inches as unit of measure
pdf=FPDF(format='letter', unit='in')
# Add new page. Without this you cannot create the document.
pdf.add_page()
# Set font face to Times, size 10.0 pt
pdf.set_font('Times','',10.0)

# Set color navy
pdf.set_text_color(*name_to_rgb('navy'))    
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.15)

# Set color dark olive green
pdf.set_text_color(*name_to_rgb('darkolivegreen'))   
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.15)

# Set color salmon
pdf.set_text_color(*name_to_rgb('salmon'))   
pdf.cell(1.0,0.0,'Hello World!')
pdf.ln(0.15)

pdf.output('hello4.pdf','F')

helloworld4

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s