Python Library: termtables | Pretty tables in terminal

06 Sep 2022

Library resources
PyPI https://pypi.org/project/termtables/
Github https://github.com/nschloe/termtables

pip3 install termtables

Example

import termtables as tt

# define header row of pretty table with a list
header = ['email', 'first']

# empty list that will be populated and printed
list_to_print = []

# data
data = grist_xx.xxx.fetch_table('Master')

count_empty_first_name = 0

for contact in data:
    if contact.first == '':
        first = my_utils.first_from_email(contact.email)
        # appending the email and first name pairs as tuple to the list
        list_to_print.append((first, contact.email))

# pretty printing the table using the tuples in the list
tt.print(
list_to_print,
header=header,
style=tt.styles.markdown,
padding=(0, 1),
# alignment="lcr"
)

outputs:

output

Styles

plain - a plain style without any additional formatting.
single - a style with single lines.
single_double - a style with single horizontal and double vertical lines.
double - a style with double lines.
rounded - a style with rounded corners and single lines.
markdown - a style resembling Markdown tables.
tsv - a style similar to tab-separated values format.
html - a style that generates an HTML table.
pipe - a style with pipe-separated values.
orgtbl - a style resembling org-mode tables in Emacs.
rst - a style resembling reStructuredText tables.

import termtables as tt

data = [("Header 1", "Header 2"), ("Row 1, Col 1", "Row 1, Col 2"), ("Row 2, Col 1", "Row 2, Col 2")]

tt.print(
    data,
    style=tt.styles.rounded,  # Change this to any available style
)

links

social