← Back

table2string

GitHub Badge PyPi Package Version
Python library for converting tables to strings - with convenient support:
  • Line breaks within cells
  • Nested tables that merge nicely at their borders
  • Horizontal and vertical alignments
  • Emoji integrations
  • Support for ANSI colors, formatting and links
Please star this project on GitHub! ⭐
formatting_example_IPython.png

Quick start

Download the library

pip install -U table2string

You can specify parameters name and column_names

from table2string import Table
Table(
    name="Table Name",
    column_names=("c1", "c2", "c3"),
    table=[
        ("1", "2", "3"),
        ("qwe", "rty\nuio", ""),
    ],
).print()

+----------------+
|   Table Name   |
+-----+-----+----+
c1  | c2  | c3 |
+-----+-----+----+
|   1 |   2 |  3 |
+-----+-----+----+
| qwe | rty |    |
|     | uio |    |
+-----+-----+----+

Import and Export Options

You can load data by calling Table(), or use the static method .from_table, as well as .from_csv for CSV files or .from_db_cursor for database cursors

You can export data with .print to the console or a file, or with .stringify to get a string

During export, you can specify alignment, border style, and cells width and height

aligns.png

Aligns

Horizontal Alignment

The * symbol indicates type-dependent alignment. If this is a number and there are no line breaks in this cell, then align to the right; otherwise, align to the left. The <, ^, and > symbols are used for left, center, and right alignment, respectively

Vertical Alignment

The ^ symbol stands for top alignment, - for center alignment, and _ for bottom alignment

You can also use constants from the HorizontalAlignment and VerticalAlignment enums instead of symbols

Code from the picture
from functools import partial
from table2string import Table, Themes
sub_table_auto_func = partial(Table, [("123", "text",)], max_height=9, maximize_height=True)
sub_table_func = partial(Table, [("first line\ntext",),], max_height=9, maximize_height=True)
Table(
    [
        *(
            [v_align, sub_table_auto_func(h_align="*", v_align=v_align)] + [
                sub_table_func(h_align=h_align, v_align=v_align)
                for h_align in ("<", ">", "^", "^<", "^>")
            ]
            for v_align in ("^", "-", "_")
        )
    ],
    column_names=(" ", "*", "<", ">", "^", "^<", "^>"),
).print(max_width=(1, len("first line") + 4), v_align=("-",), theme=Themes.thin_thick)
borders.png

Borders

When exporting, you can specify the theme parameter with one of the following options: ascii_thin, ascii_thin_double, ascii_double, ascii_double_thin, thin, thin_thick, thin_double, rounded_double, rounded, rounded_thick, thick, thick_thin, double, double_thin, booktabs, ascii_booktabs, markdown

These themes are available through the Themes class. For example: Themes.thin_thick

You can also create your own themes using the Theme class

Code from the picture
from table2string import Table, Themes, HorizontalAlignment
table = []
example_table = Table([(" ", " "), (" ", " "), (" ", " ")])
theme_names = (
    ("ascii_thin", "ascii_thin_double", "ascii_double", "ascii_double_thin", "thin", "thin_thick"),
    ("thin_double", "rounded_double", "rounded", "rounded_thick", "thick", "thick_thin"),
    ("double", "double_thin", "booktabs", "ascii_booktabs", "markdown", "None"),
)
for names in theme_names:
    table.append([])
    for name in names:
        string_table = example_table.stringify(
            theme=getattr(Themes, name, Themes.ascii_thin)
        )
        table[-1].append(f"{name}\n{string_table}")
Table(table).print(theme=Themes.thin, h_align=HorizontalAlignment.CENTER)
subtable_example.png

Subtable

You can include a table inside a table

from table2string import Table, Themes
subtable = Table(
    name="SubTable",
    table=[("1", "2"), ("3", "4")],
)
table = Table(
    name="Table Name",
    table=[("c1", subtable)],
)
table.print(
    v_align=("-",),
    max_width=(2, 8),
    theme=Themes.thin_thick,
)
formatting_example.png

Formatting

You can format the text using ANSI codes (color, links, etc.) or write your own formatting, for example HTML.

All formatting works correctly with line breaks and alignment

from table2string import Table, Themes, style, link, Color
red_text = style("w\ne", fg=Color.RED)
example_link = link("https://example.com", "link")
text = f"q{red_text}rty{example_link}"
Table([(text,)]).print(
    max_width=(3,),
    theme=Themes.thin_thick,
)
← Back