{ "metadata": { "name": "", "signature": "sha256:671fcf53b684033c2b56b2d3722eb14d5fedade2b71d8cf2fb4eaaff8e33da86" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import pandas as pd\n", "from reportlab.lib import colors\n", "import tia.rlab as rlab" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "df = pd.DataFrame({'col1': [.1, .2, .3], 'col2': [10000000, 20000000, 30000000]}, index=['A', 'B', 'C'])\n", "df.columns = pd.MultiIndex.from_arrays([['h1'] * 2, df.columns.values])\n", "df" ], "language": "python", "metadata": {}, "outputs": [ { "html": [ "
\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
h1
col1col2
A 0.1 10000000
B 0.2 20000000
C 0.3 30000000
\n", "
" ], "metadata": {}, "output_type": "pyout", "prompt_number": 12, "text": [ " h1 \n", " col1 col2\n", "A 0.1 10000000\n", "B 0.2 20000000\n", "C 0.3 30000000" ] } ], "prompt_number": 12 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Show the different simple table stylings available" ] }, { "cell_type": "code", "collapsed": false, "input": [ "#\n", "# Build the pdf template and then the pdf\n", "# \n", "pdf_path = 'rlab_table_style.pdf'\n", "pdf = rlab.PdfBuilder(pdf_path, showBoundary=1)\n", "pdf.define_simple_grid_template('t1', 3, 3)\n", "\n", "def build_itms(lbl, cb):\n", " tf = pdf.table_formatter(df)\n", " cb and cb(tf)\n", " tf.cells.iloc[:, 0].percent_format()\n", " tf.cells.iloc[:, 1].millions_format()\n", " return [pdf.p('{0}
 
'.format(lbl)), tf.build(expand=None, shrink=None)]\n", "\n", "\n", "# Try some other fonts\n", "pfont = rlab.Font('Palatino Linotype', 'pala.ttf').try_load(default='Helvetica')\n", "pfontb = rlab.Font('Palatino Linotype Bold', 'palab.ttf').try_load(default='Helvetica')\n", " \n", "\n", "f0 = None\n", "f1 = lambda tf: tf.apply_basic_style()\n", "# Make the table purple\n", "f2 = lambda tf: tf.apply_basic_style(cmap=rlab.Style.Black)\n", "f3 = lambda tf: tf.apply_basic_style(cell_border_clazz=rlab.BorderTypeHorizontal, hdr_border_clazz=rlab.BorderTypeHorizontal, cmap=rlab.Style.Cyan)\n", "f4 = lambda tf: tf.apply_basic_style(stripe_rows=0, cell_border_clazz=rlab.BorderTypeHorizontal, hdr_border_clazz=rlab.BorderTypeHorizontal, cmap=rlab.Style.Lime)\n", "f5 = lambda tf: tf.apply_basic_style(font_size=12, font=pfont, font_bold=pfontb, stripe_rows=0, cell_border_clazz=rlab.BorderTypeHorizontal, \n", " hdr_border_clazz=rlab.BorderTypeHorizontal, cmap=rlab.Style.Purple)\n", "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)\n", "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)\n", "\n", "def f8(tf):\n", " # more complex as you want to also make index dark blue\n", " tf.apply_basic_style(colspans=0, cell_border_clazz=rlab.BorderTypeOutline, hdr_border_clazz=rlab.BorderTypeGrid, stripe_rows=1, cmap=rlab.Style.Blue)\n", " tf.index.set_background(rlab.Style.Blue['Dark'])\n", " tf.index.set_textcolor(colors.white)\n", "\n", "rlab.Font\n", "\n", "#pdf.build_page('t1', {'0,0': tf.build(expand='wh', shrink='wh')})\n", "itms = {}\n", "itms['0,0'] = build_itms('No Style', f0)\n", "itms['0,1'] = build_itms('Default', f1)\n", "itms['0,2'] = build_itms('Black', f2)\n", "itms['1,0'] = build_itms('Cyan w/ Horizontal Borders', f3)\n", "itms['1,1'] = build_itms('Lime w/ Horizontal Borders and No Stripes', f4)\n", "itms['1,2'] = build_itms('Purple w/ Palatino Font and Larger Font Size', f5)\n", "\n", "itms['2,0'] = build_itms('Orange w/ Vertical Borders', f6)\n", "itms['2,1'] = build_itms('Red w/ Grid', f7)\n", "itms['2,2'] = build_itms('Blue w/ Outline and Index Background', f8)\n", "\n", "pdf.build_page('t1', itms)\n", "#pdf.build_page('t1', {'0,1': tf.build(expand='wh', shrink=None), '0,0': tf.build(expand='wh', shrink=None)})\n", "pdf.save()\n" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 13 }, { "cell_type": "heading", "level": 3, "metadata": {}, "source": [ "Open the directory to find the rlab_table_style.pdf" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "[Click ME](https://github.com/bpsmith/tia/raw/master/examples/rlab_table_style.pdf)." ] }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }