In [1]:
import pandas as pd
from reportlab.lib import colors
import tia.rlab as rlab

In [12]:
df = pd.DataFrame({'col1': [.1, .2, .3], 'col2': [10000000, 20000000, 30000000]}, index=['A', 'B', 'C'])
df.columns = pd.MultiIndex.from_arrays([['h1'] * 2, df.columns.values])
df


Out[12]:
h1
col1 col2
A 0.1 10000000
B 0.2 20000000
C 0.3 30000000

Show the different simple table stylings available


In [13]:
#
# Build the pdf template and then the pdf
# 
pdf_path = 'rlab_table_style.pdf'
pdf = rlab.PdfBuilder(pdf_path, showBoundary=1)
pdf.define_simple_grid_template('t1', 3, 3)

def build_itms(lbl, cb):
    tf = pdf.table_formatter(df)
    cb and cb(tf)
    tf.cells.iloc[:, 0].percent_format()
    tf.cells.iloc[:, 1].millions_format()
    return [pdf.p('<b><u>{0}</u></b><br/>&nbsp;<br/>'.format(lbl)), tf.build(expand=None, shrink=None)]


# Try some other fonts
pfont =  rlab.Font('Palatino Linotype', 'pala.ttf').try_load(default='Helvetica')
pfontb =  rlab.Font('Palatino Linotype Bold', 'palab.ttf').try_load(default='Helvetica')
    

f0 = None
f1 = lambda tf: tf.apply_basic_style()
# Make the table purple
f2 = lambda tf: tf.apply_basic_style(cmap=rlab.Style.Black)
f3 = lambda tf: tf.apply_basic_style(cell_border_clazz=rlab.BorderTypeHorizontal, hdr_border_clazz=rlab.BorderTypeHorizontal, cmap=rlab.Style.Cyan)
f4 = lambda tf: tf.apply_basic_style(stripe_rows=0, cell_border_clazz=rlab.BorderTypeHorizontal, hdr_border_clazz=rlab.BorderTypeHorizontal,  cmap=rlab.Style.Lime)
f5 = lambda tf: tf.apply_basic_style(font_size=12, font=pfont, font_bold=pfontb, stripe_rows=0, cell_border_clazz=rlab.BorderTypeHorizontal, 
                                     hdr_border_clazz=rlab.BorderTypeHorizontal, cmap=rlab.Style.Purple)
f6 = lambda tf: tf.apply_basic_style(colspans=0, cell_border_clazz=rlab.BorderTypeVertical, hdr_border_clazz=rlab.BorderTypeVertical, stripe_cols=1, cmap=rlab.Style.Orange)
f7 = lambda tf: tf.apply_basic_style(colspans=0, cell_border_clazz=rlab.BorderTypeGrid, hdr_border_clazz=rlab.BorderTypeGrid, stripe_rows=0, cmap=rlab.Style.Red)

def f8(tf):
    # more complex as you want to also make index dark blue
    tf.apply_basic_style(colspans=0, cell_border_clazz=rlab.BorderTypeOutline, hdr_border_clazz=rlab.BorderTypeGrid, stripe_rows=1, cmap=rlab.Style.Blue)
    tf.index.set_background(rlab.Style.Blue['Dark'])
    tf.index.set_textcolor(colors.white)

rlab.Font

#pdf.build_page('t1', {'0,0': tf.build(expand='wh', shrink='wh')})
itms = {}
itms['0,0'] = build_itms('No Style', f0)
itms['0,1'] = build_itms('Default', f1)
itms['0,2'] = build_itms('Black', f2)
itms['1,0'] = build_itms('Cyan w/ Horizontal Borders', f3)
itms['1,1'] = build_itms('Lime w/ Horizontal Borders and No Stripes', f4)
itms['1,2'] = build_itms('Purple w/ Palatino Font and Larger Font Size', f5)

itms['2,0'] = build_itms('Orange w/ Vertical Borders', f6)
itms['2,1'] = build_itms('Red w/ Grid', f7)
itms['2,2'] = build_itms('Blue w/ Outline and Index Background', f8)

pdf.build_page('t1', itms)
#pdf.build_page('t1', {'0,1': tf.build(expand='wh', shrink=None), '0,0': tf.build(expand='wh', shrink=None)})
pdf.save()

Open the directory to find the rlab_table_style.pdf


In [ ]: