Table

class pdfpug.modules.Table(data, **kwargs)

A Table lists data in organised manner making it easier to digest large amounts of data. It is made up of Row and Cell as shown in the screenshot.

It is also worth noting that the header and body of a table are also comprised of the same. The header and body attributes exist primarily for style changes. Header contents have a stronger style by being in bold and allow the reader to be informed of what the categories of data are. The body counterpart places more emphasis on placing the content in an organised manner so to speak.

../_images/table.png
Parameters:
  • header (Optional[List]) – Header row
  • data (List[List]) – Body rows
  • spacing (TableSpacing) – Table spacing (defaults to TableSpacing.comfortable)
  • striped (Optional[TableRowStyle]) – Table row style
  • table_type (TableType) – Table type (defaults to TableType.celled)
  • color (Optional[Color]) – Table color
  • column_width_rule (Optional[TableColumnWidth]) – Table column width

A simple table consisting of just strings and numbers can be created as shown below.

>>> from pdfpug.modules import Table
>>> basic_table = Table(
...    header=['Serial No.', 'Fruit', 'Stock Level'],
...    data=
...     [
...         [1, 'Apple', 'Low'],
...         [2, 'Orange', 'Low'],
...         [3, 'Grape', 'High'],
...         [4, 'Guava', 'Not Available']
...     ],
... )
../_images/basic_table.png

More formatting options are unlocked if the Row and Cell are used. A Cell allows for embedding of other elements like Header etc thereby providing more control of the content layouts and style.

A more advanced table would looks something like the following where the cell content alignment is modified. Also, the table has alternate row colored different and uses a compact style.

>>> from pdfpug.modules import Cell, Row
>>> from pdfpug.common import TableSpacing, TableRowStyle, State, Alignment
>>> advanced_table = Table(
>>>     header=['Player', 'Hero', 'Role', 'K/D/A'],
>>>     data=
...     [
...         Row(
...             ['Kuro', 'Lion', Cell('Support', row_span=2), '2/10/15'],
...             alignment=Alignment.center,
...             state=State.negative
...         ),
...         Row(['Gh', 'Oracle', '3/7/6'], alignment=Alignment.center),
...         Row(['Miracle', 'Void', 'Carry', '9/2/4'], alignment=Alignment.center),
...         Row(['W33', 'Timber', 'Midlaner', '5/8/2'], alignment=Alignment.center)
...     ],
...     spacing=TableSpacing.compact,
...     striped=TableRowStyle.striped,
... )
../_images/advanced_table.png
class pdfpug.modules.Row(data, **kwargs)

A Row is the next higher order element above Cell. Multiple Rows make up a Table similar to how multiple Cell make a Row.

Parameters:
  • data (List) – Row contents
  • row_type (TableRowType) – Row type (defaults to TableRowType.body)
  • state (Optional[State]) – Row state
  • alignment (Optional[Alignment]) – Horizontal alignment of row contents
>>> from pdfpug.modules import Row, Cell, Header
>>> row = Row(
...     ['Cell 1', 'Cell 2', Cell(Header('Inception'))], alignment=Alignment.left
... )
class pdfpug.modules.Cell(data, **kwargs)

A Cell is the most basic unit (lowest denominator) of a Table. A group of cells together form a Row.

Parameters:
  • BasePugElement] data (Union[str,) – Cell content
  • cell_type (TableRowType) – Cell type (defaults to TableRowType.body)
  • width (Optional[int]) – Cell width (should be in the range of 1-16 & only set for TableRowType.header cell type)
  • row_span (Optional[int]) – Cell span across rows
  • column_span (Optional[int]) – Cell span across columns
  • state (Optional[State]) – Cell content state
  • alignment (Optional[Alignment]) – Cell content horizontal alignment

It can contain a simple string to complex elements like Header, OrderedList etc. This allows for embedding all kinds of data in a Cell.

>>> from pdfpug.modules import Cell, Header
>>> header_cell = Cell(Header('Header Inside Cell'))

A Cell has various customisation attributes that enable data to be represented accurately. For instance, if certain content need to be represented positively, one can do the following,

>>> from pdfpug.common import State
>>> pos_cell = Cell('Available', state=State.positive)
class pdfpug.common.TableType

Enum Table types

celled = 'celled'

Default table style with each cell clearly visible due to separators

../_images/celled_table_style.png
simple = 'basic'

Bare minimum row separating lines with table border

../_images/simple_table_style.png
bare = 'very basic'

Bare minimum row separating lines and no table border

../_images/bare_table_style.png
class pdfpug.common.TableColumnWidth

Enum Table column width rules

fixed = 'fixed'

Equal widths for all columns

../_images/celled_table_style.png
minimum = 'collapsing'

Minimum width for each column based on their content

../_images/minimum_table_column_width.png
class pdfpug.common.TableSpacing

Enum Table row spacing

tight = 'very compact'

Tight spacing of row content

compact = 'compact'

Compact spacing of row content

comfortable = 'padded'

Good spacing of row content

spacious = 'very padded'

Spacious padding of row content

class pdfpug.common.TableRowStyle

Table row style

striped = 'striped'

Set if alternate rows should be colored differently

class pdfpug.common.TableRowType

Table row type

header = 'th'

Header row

body = 'td'

Body row