Tuesday, June 28, 2011

Restructured text table reports

Download: rst2pdf-read-only.tar.gz

I use rst2pdf to create PDF documents from reStructuredText (ReST). It is relatively easy to generate table reports from a database, like the ones shown below. In this example, the table does not fit into one page and spans two.




Although it is easy to execute statistical queries such as sum a column in a database table using SQL, it is not possible to add summary rows to each page when a table spans multiple pages. The shortcoming with ReST and rst2pdf is that you do not know how many rows will fit into a page because it depends on the page size (A4 etc), font size and table content. The modified version of rst2pdf I provide in this post allows you to attach additional rows (with sum of column information) onto the bottom of tables, automatically, and for each page.  In the example you can see how to insert current page sum of column, previous pages sum of column and total pages sum of column information into the summary row. The ":sum:" is a nonstandard interpreted text role, which means it will only work with this version of rst2pdf.

The syntax to generate the table is this:

.. list-table:: Title
   :widths: 15 10 30
   :header-rows: 1

   * - Description
     - Weight
     - Height
   * - A
     - 1
     - 2 
   * - B
     - 1
     - 2
   * - C
     - 1
     - 2
   * - D
     - 1
     - 2
   * - E
     - 1
     - 2
   * - F
     - 1
     - 2 
   * - G
     - 1
     - 2
   * - H
     - 1
     - 2
   * - I
     - 1
     - 2 
   * - J
     - 1
     - 2
   * - k
     - 1
     - 2
   * - L
     - 1
     - 2
   * - M
     - 1
     - 2
   * - N
     - 1
     - 2
   * - O
     - 1
     - 2
   * - P
     - 1
     - 2
   * - Q
     - 1
     - 2
   * - R
     - 1
     - 2
   * - Previous Total 
     - :sum:`previous_pages_col`
     - :sum:`previous_pages_col`
   * - Current Page Total 
     - :sum:`current_pages_col`
     - :sum:`current_pages_col`
   * - Total 
     - :sum:`total_pages_col`
     - :sum:`total_pages_col`


The command to create the table:

python createpdf.py --repeat-table-rows table.txt -o table.pdf

Socializer Widget By Blogger Yard
SOCIALIZE IT →
FOLLOW US →
SHARE IT →

0 comments:

Post a Comment