A lightweight Python library for generating ASCII tables that automatically adjust their column widths to fit your data.
pip install python-table-print
PrintTable is the core class — add rows to it, then call get_table() to retrieve the formatted table as a string:
from table import PrintTable, Justification
schedule = PrintTable()
schedule.set_title("Solar System at a Glance")
schedule.add_row("Planet", "Type", "Moons", "Day Length", "Claim to Fame")
schedule.add_row("Mercury", "Terrestrial", "0", "59 days", "Closest to the Sun")
schedule.add_row("Venus", "Terrestrial", "0", "243 days", "Hottest surface")
schedule.add_row("Earth", "Terrestrial", "1", "24 hours", "Only known life")
schedule.add_row("Mars", "Terrestrial", "2", "24.6 hours", "Home of Olympus Mons")
schedule.add_row("Jupiter", "Gas giant", "95", "9.9 hours", "Largest planet")
schedule.add_row("Saturn", "Gas giant", "146", "10.7 hours", "The ringed beauty")
schedule.add_row("Uranus", "Ice giant", "28", "17.2 hours", "Rotates on its side")
schedule.add_row("Neptune", "Ice giant", "16", "16.1 hours", "Farthest from Sun")
schedule.set_column_justification(2, Justification.RIGHT)
print(schedule.get_table())┌───────────────────────────────────────────────────────────────────┐
│ Solar System at a Glance │
├─────────┬─────────────┬───────┬────────────┬──────────────────────┤
│ Planet │ Type │ Moons │ Day Length │ Claim to Fame │
├─────────┼─────────────┼───────┼────────────┼──────────────────────┤
│ Mercury │ Terrestrial │ 0 │ 59 days │ Closest to the Sun │
│ Venus │ Terrestrial │ 0 │ 243 days │ Hottest surface │
│ Earth │ Terrestrial │ 1 │ 24 hours │ Only known life │
│ Mars │ Terrestrial │ 2 │ 24.6 hours │ Home of Olympus Mons │
│ Jupiter │ Gas giant │ 95 │ 9.9 hours │ Largest planet │
│ Saturn │ Gas giant │ 146 │ 10.7 hours │ The ringed beauty │
│ Uranus │ Ice giant │ 28 │ 17.2 hours │ Rotates on its side │
│ Neptune │ Ice giant │ 16 │ 16.1 hours │ Farthest from Sun │
└─────────┴─────────────┴───────┴────────────┴──────────────────────┘
get_table() returns the table as a plain string, so it can be passed to any function — not just print.
By default, the first row is treated as a header and rendered with a separator below it. This can be toggled via the has_header_row property:
my_table.has_header_row = FalseA title can be added above the table with set_title(). It defaults to centre justification:
my_table.set_title("My Title")To remove the title, call clear_title().
By default, all cells are left-justified. Justification can be set at four levels of granularity — each level overrides any previously set justification for the affected cells. The Justification enum must be imported alongside PrintTable.
from table import PrintTable, Justification
my_table.set_table_justification(Justification.CENTRE)my_table.set_row_justification(0, Justification.LEFT)my_table.set_column_justification(0, Justification.RIGHT)my_table.set_cell_justification(1, 1, Justification.LEFT)Justification for the title can also be set independently:
my_table.set_title_justification(Justification.LEFT)