Skip to main content

Allows for pretty printing and automatic resizing of tables.

Project description

PyPi Version python version downloads codecov

Python Table Print

A lightweight Python library for generating ASCII tables that automatically adjust their column widths to fit your data.

Installation

pip install python-table-print

Quick Start

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.

Usage

Header Row

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 = False

Title

A 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().

Justification

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.

Table-wide

from table import PrintTable, Justification

my_table.set_table_justification(Justification.CENTRE)

Row

my_table.set_row_justification(0, Justification.LEFT)

Column

my_table.set_column_justification(0, Justification.RIGHT)

Cell

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)

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

python_table_print-1.0.5.tar.gz (10.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

python_table_print-1.0.5-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file python_table_print-1.0.5.tar.gz.

File metadata

  • Download URL: python_table_print-1.0.5.tar.gz
  • Upload date:
  • Size: 10.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for python_table_print-1.0.5.tar.gz
Algorithm Hash digest
SHA256 d8ad9dd284c310a481d545330ef4fc50a63fab6657b7f4ca18a87ede655b6e7e
MD5 a60a98e23bf2d23d81267b1acf5bf47a
BLAKE2b-256 90928c648c65bc111b95c4ce87177f5c126785a24c299bfa97bc43c60597044a

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_table_print-1.0.5.tar.gz:

Publisher: python-publish.yml on Zandwhich/Python-Table-Print

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_table_print-1.0.5-py3-none-any.whl.

File metadata

File hashes

Hashes for python_table_print-1.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 6b62469fb96ab380cda37a9177e51bdffaa7842a4bfa7d8614bb3143405cef3a
MD5 816fcb94e3458db167e61e87c4011393
BLAKE2b-256 96698e2300b974cd43646163ac1a403489d079e1617bca4d8312a7034b5bd15d

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_table_print-1.0.5-py3-none-any.whl:

Publisher: python-publish.yml on Zandwhich/Python-Table-Print

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page