While there are several libraries available for converting tables to strings in Python, none seemed to meet my specific requirements.
- Line Break Support: Easily include line breaks within cells for enhanced readability.
- Emoji Integration: Effortlessly incorporate emoji characters into your tables to add visual appeal and context.
pip install -U table2string
pip install -U git+https://github.com/EgorKhabarov/table2string.git@master
>>> from table2string import print_table, stringify_table
>>> print_table([("1", "2", "3"), ("qwe", "rty\nuio", "")], name="Table Name")
+---------------+
| Table Name |
+-----+-----+---+
| 1 | 2 | 3 |
+-----+-----+---+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> print(stringify_table([("1", "2", "3"), ("qwe", "rty\nuio", "")], name="Table Name"))
+---------------+
| Table Name |
+-----+-----+---+
| 1 | 2 | 3 |
+-----+-----+---+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> from io import StringIO
>>> from table2string import Table
>>> Table.from_csv(StringIO('c1,c2,c3\n1,2,3\nqwe,"rty\nuio",'), name="Table Name").print()
+----------------+
| Table Name |
+-----+-----+----+
| c1 | c2 | c3 |
+-----+-----+----+
| 1 | 2 | 3 |
+-----+-----+----+
| qwe | rty | |
| | uio | |
+-----+-----+----+
>>> Table.from_csv(StringIO('c1,c2,c3\n1,2,3\nqwe,"rty\nuio",'), name="Table Name", column_names=False).print()
+---------------+
| Table Name |
+-----+-----+---+
| 1 | 2 | 3 |
+-----+-----+---+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> import sqlite3
>>> cursor = sqlite3.connect(":memory:").cursor()
>>> cursor.execute(
... "CREATE TABLE data (c1 TEXT, c2 TEXT, c3 TEXT);"
... ).executemany(
... "INSERT INTO data (c1, c2, c3) VALUES (?, ?, ?);",
... [("1", "2", "3"), ("qwe", "rty\nuio", "")],
... ).execute(
... "SELECT c1, c2, c3 FROM data;"
... ) and None # because this method returns a cursor
>>> Table.from_db_cursor(cursor, name="Table Name").print()
+---------------+
| Table Name |
+-----+-----+---+
| 1 | 2 | 3 |
+-----+-----+---+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> cursor.execute("SELECT c1, c2, c3 FROM data;") and None
>>> Table.from_db_cursor(cursor, name="Table Name", column_names=True).print()
+----------------+
| Table Name |
+-----+-----+----+
| c1 | c2 | c3 |
+-----+-----+----+
| 1 | 2 | 3 |
+-----+-----+----+
| qwe | rty | |
| | uio | |
+-----+-----+----+
Width | Example | Description |
---|---|---|
<width> |
10 |
Setting width (10 ) for the whole table |
(<width>,) |
(10,) |
Setting width_1 (10 ) for all column |
(<width_1>, <width_2>) |
(10, 20) |
Setting width_1 (10 ) for the first column and width_2 (20 ) for all other columns |
(<width_1>, <width_2>, <width_3>) |
(10, 20, 30) |
Setting width_1 (10 ) for the first column and width_2 (20 ) for the second and width_3 (30 ) for the third column |
Example
>>> # Width of the entire table with borders
>>> print_table([(1,), (2.345,), ("example",)], max_width=10)
+--------+
| 1 |
+--------+
| 2.345 |
+--------+
| examplโฉ|
| e |
+--------+
>>> # Width of each column individually
>>> print_table([(1,), (2.345,), ("example",)], max_width=(10,))
+------------+
| 1 |
+------------+
| 2.345 |
+------------+
| example |
+------------+
>>> print_table([("123456\n\n789000", "example")], max_width=(3, 4), max_height=4)
+-----+------+
| 123โฉ| examโฉ|
| 456 | ple |
| | |
| 789โฆ| |
+-----+------+
>>> print_table([("123456789",)], max_width=(1,), max_height=1)
+---+
| 1โฆ|
+---+
>>> print_table(
... table=[("123\n456\n789",)],
... max_width=(3,),
... max_height=4,
... maximize_height=True,
... )
+-----+
| 123 |
| 456 |
| 789 |
| |
+-----+
>>> print_table(
... table=[("123456789",)],
... max_width=(3,),
... max_height=4,
... maximize_height=True,
... )
+-----+
| 123โฉ|
| 456โฉ|
| 789 |
| |
+-----+
Align | Example | Description |
---|---|---|
"<align>" or ("<align>",)
|
"^" or ("^",)
|
Setting align ("^" ) for all columns |
("<align_1>", "<align_2>") |
("^", "<") |
Setting align_1 ("^" ) for the first column and align_2 ("<" ) for all other columns |
("<align_1>", "<align_2>", "<align_3>") |
("^", "<", ">") |
Setting align_1 ("^" ) for the first column and align_2 ("<" ) for the second and align_3 (">" ) for the third column |
Align | Description |
---|---|
* or **
|
Alignment depends on the type. If this is a number and there are no line breaks in this cell, then align to the right; otherwise, align to the left. |
< or <<
|
All lines are left aligned |
^ or ^^
|
All lines are center aligned |
> or >>
|
All lines are right aligned |
<^ |
The first line is left aligned and the remaining lines are centered |
<> |
The first line is left aligned and the remaining lines are right aligned |
^< |
The first line is aligned to the center, and the remaining lines are aligned to the left of the first line. |
^> |
The first line is aligned to the center, and the remaining lines are aligned to the right of the first line. |
>< |
The first line is right aligned and the remaining lines are left aligned |
>^ |
The first line is right aligned and the remaining lines are centered |
Example
>>> kwargs_1 = {
... "table": [("1", "123456789\nqwerty\nasdfghjklzxcvb")],
... "name": "Table Name\nName\nNaaaaame",
... "column_names": ("1", "col 2\nc2"),
... "max_width": (5, 15),
... }
>>> print_table(**kwargs_1)
+-------------------------+
| Table Name |
| Name |
| Naaaaame |
+-------+-----------------+
| 1 | col 2 |
| | c2 |
+-------+-----------------+
| 1 | 123456789 |
| | qwerty |
| | asdfghjklzxcvb |
+-------+-----------------+
>>> print_table(**kwargs_1, align="*", name_align="*", column_names_align="*") # align="**", name_align="**", column_names_align="**"
+-------------------------+
| Table Name |
| Name |
| Naaaaame |
+-------+-----------------+
| 1 | col 2 |
| | c2 |
+-------+-----------------+
| 1 | 123456789 |
| | qwerty |
| | asdfghjklzxcvb |
+-------+-----------------+
>>> print_table(**kwargs_1, align="<", name_align="<", column_names_align="<") # align="<<", name_align="<<", column_names_align="<<"
+-------------------------+
| Table Name |
| Name |
| Naaaaame |
+-------+-----------------+
| 1 | col 2 |
| | c2 |
+-------+-----------------+
| 1 | 123456789 |
| | qwerty |
| | asdfghjklzxcvb |
+-------+-----------------+
>>> print_table(**kwargs_1, align=">", name_align=">", column_names_align=">") # align=">>", name_align=">>", column_names_align=">>"
+-------------------------+
| Table Name |
| Name |
| Naaaaame |
+-------+-----------------+
| 1 | col 2 |
| | c2 |
+-------+-----------------+
| 1 | 123456789 |
| | qwerty |
| | asdfghjklzxcvb |
+-------+-----------------+
>>> print_table(**kwargs_1, align="^", name_align="^", column_names_align="^") # align="^^", name_align="^^", column_names_align="^^"
+-------------------------+
| Table Name |
| Name |
| Naaaaame |
+-------+-----------------+
| 1 | col 2 |
| | c2 |
+-------+-----------------+
| 1 | 123456789 |
| | qwerty |
| | asdfghjklzxcvb |
+-------+-----------------+
>>> print_table(**kwargs_1, align="^<", name_align="^<", column_names_align="^<")
+-------------------------+
| Table Name |
| Name |
| Naaaaame |
+-------+-----------------+
| 1 | col 2 |
| | c2 |
+-------+-----------------+
| 1 | 123456789 |
| | qwerty |
| | asdfghjklzxcvb |
+-------+-----------------+
>>> print_table(**kwargs_1, align="^>", name_align="^>", column_names_align="^>")
+-------------------------+
| Table Name |
| Name |
| Naaaaame |
+-------+-----------------+
| 1 | col 2 |
| | c2 |
+-------+-----------------+
| 1 | 123456789 |
| | qwerty |
| | asdfghjklzxcvb |
+-------+-----------------+
>>> print_table([("qwerty\n123456789\nasdfghjklzxcvb",)], max_width=(18,), align="^<")
+--------------------+
| qwerty |
| 123456789 |
| asdfghjklzxcvb |
+--------------------+
>>> print_table([("qwerty\n123456789\nasdfghjklzxcvb",)], max_width=(18,), align="^>")
+--------------------+
| qwerty |
| 123456789 |
| asdfghjklzxcvb |
+--------------------+
Separator | Description |
---|---|
sep=True |
All horizontal dividers included |
sep=False |
All horizontal dividers are disabled |
sep=(1,) |
Only first delimiter |
sep=(1, 3, 5) |
Only first third and fifth separator |
sep=range(1, 100, 5) |
Delimiter every five lines first 100 lines |
Example
>>> table_1 = [("qwe", "rty\nuio"), ("123456\n\n789000", "example")]
>>> kwargs = {
... "max_width": (3, 4),
... "max_height": 4,
... }
>>> print_table(table_1, **kwargs, sep=True)
+-----+------+
| qwe | rty |
| | uio |
+-----+------+
| 123โฉ| examโฉ|
| 456 | ple |
| | |
| 789โฆ| |
+-----+------+
>>> print_table(table_1, **kwargs, sep=False)
+-----+------+
| qwe | rty |
| | uio |
| 123โฉ| examโฉ|
| 456 | ple |
| | |
| 789โฆ| |
+-----+------+
>>> table_2 = [("1", "2"), ("3", "4")]
>>> print_table(table_2, sep=True, name="Name")
+-------+
| Name |
+---+---+
| 1 | 2 |
+---+---+
| 3 | 4 |
+---+---+
>>> print_table(table_2, sep=False, name="Name")
+-------+
| Name |
+---+---+
| 1 | 2 |
| 3 | 4 |
+---+---+
>>> table_3 = [("1", "2"), ("3", "4"), ("5", "6"), ("7", "8")]
>>> print_table(table_3, sep=(1,))
+---+---+
| 1 | 2 |
+---+---+
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
+---+---+
>>> print_table(table_3, sep=(2,))
+---+---+
| 1 | 2 |
| 3 | 4 |
+---+---+
| 5 | 6 |
| 7 | 8 |
+---+---+
>>> print_table(table_3, sep=(1, 3))
+---+---+
| 1 | 2 |
+---+---+
| 3 | 4 |
| 5 | 6 |
+---+---+
| 7 | 8 |
+---+---+
>>> print_table(table_3, sep=(1,), name="Name")
+-------+
| Name |
+---+---+
| 1 | 2 |
+---+---+
| 3 | 4 |
| 5 | 6 |
| 7 | 8 |
+---+---+
>>> print_table(table_3, sep=(2,), name="Name")
+-------+
| Name |
+---+---+
| 1 | 2 |
| 3 | 4 |
+---+---+
| 5 | 6 |
| 7 | 8 |
+---+---+
>>> print_table(table_3, sep=(1, 3), name="Name")
+-------+
| Name |
+---+---+
| 1 | 2 |
+---+---+
| 3 | 4 |
| 5 | 6 |
+---+---+
| 7 | 8 |
+---+---+
Border types
โญโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโฌโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ ascii_thin โ +---------+---------+ โ +-------------------+ โ
โ โ | _ | _ | โ | ascii_thin | โ
โ โ +---------+---------+ โ +---------+---------+ โ
โ โ | _ | _ | โ | _ | _ | โ
โ โ +---------+---------+ โ +---------+---------+ โ
โ โ | _ | _ | โ | _ | _ | โ
โ โ +---------+---------+ โ +---------+---------+ โ
โ โ โ | _ | _ | โ
โ โ โ +---------+---------+ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ ascii_thin_double โ +---------+---------+ โ +-------------------+ โ
โ โ | _ | _ | โ | ascii_thin_double | โ
โ โ +=========+=========+ โ +---------+---------+ โ
โ โ | _ | _ | โ | _ | _ | โ
โ โ +---------+---------+ โ +=========+=========+ โ
โ โ | _ | _ | โ | _ | _ | โ
โ โ +---------+---------+ โ +---------+---------+ โ
โ โ โ | _ | _ | โ
โ โ โ +---------+---------+ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ ascii_double โ +=========+=========+ โ +===================+ โ
โ โ โ _ โ _ โ โ โ ascii_double โ โ
โ โ +=========+=========+ โ +=========+=========+ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ +=========+=========+ โ +=========+=========+ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ +=========+=========+ โ +=========+=========+ โ
โ โ โ โ _ โ _ โ โ
โ โ โ +=========+=========+ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ ascii_double_thin โ +=========+=========+ โ +===================+ โ
โ โ โ _ โ _ โ โ โ ascii_double_thin โ โ
โ โ +---------+---------+ โ +=========+=========+ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ +=========+=========+ โ +---------+---------+ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ +=========+=========+ โ +=========+=========+ โ
โ โ โ โ _ โ _ โ โ
โ โ โ +=========+=========+ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ ascii_booktabs โ ------------------- โ ------------------- โ
โ โ _ _ โ ascii_booktabs โ
โ โ =================== โ ------------------- โ
โ โ _ _ โ _ _ โ
โ โ ------------------- โ =================== โ
โ โ _ _ โ _ _ โ
โ โ ------------------- โ ------------------- โ
โ โ โ _ _ โ
โ โ โ ------------------- โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ thin โ โโโโโโโโโโโฌโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ โ _ โ โ โ thin โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโฌโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโดโโโโโโโโโโ โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโโดโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ thin_thick โ โโโโโโโโโโโฌโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ โ _ โ โ โ thin_thick โ โ
โ โ โโโโโโโโโโโฟโโโโโโโโโโฅ โ โโโโโโโโโโโฌโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโฟโโโโโโโโโโฅ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโดโโโโโโโโโโ โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโโดโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ thin_double โ โโโโโโโโโโโฌโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ โ _ โ โ โ thin_double โ โ
โ โ โโโโโโโโโโโชโโโโโโโโโโก โ โโโโโโโโโโโฌโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโชโโโโโโโโโโก โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโดโโโโโโโโโโ โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโโดโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ rounded โ โญโโโโโโโโโโฌโโโโโโโโโโฎ โ โญโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ โ _ โ _ โ โ โ rounded โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโฌโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โฐโโโโโโโโโโดโโโโโโโโโโฏ โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ โ _ โ _ โ โ
โ โ โ โฐโโโโโโโโโโดโโโโโโโโโโฏ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ rounded_thick โ โญโโโโโโโโโโฌโโโโโโโโโโฎ โ โญโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ โ _ โ _ โ โ โ rounded_thick โ โ
โ โ โโโโโโโโโโโฟโโโโโโโโโโฅ โ โโโโโโโโโโโฌโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโฟโโโโโโโโโโฅ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โฐโโโโโโโโโโดโโโโโโโโโโฏ โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ โ _ โ _ โ โ
โ โ โ โฐโโโโโโโโโโดโโโโโโโโโโฏ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ rounded_double โ โญโโโโโโโโโโฌโโโโโโโโโโฎ โ โญโโโโโโโโโโโโโโโโโโโโฎ โ
โ โ โ _ โ _ โ โ โ rounded_double โ โ
โ โ โโโโโโโโโโโชโโโโโโโโโโก โ โโโโโโโโโโโฌโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โโโโโโโโโโโชโโโโโโโโโโก โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โฐโโโโโโโโโโดโโโโโโโโโโฏ โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ โ _ โ _ โ โ
โ โ โ โฐโโโโโโโโโโดโโโโโโโโโโฏ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ thick โ โโโโโโโโโโโณโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ โ _ โ โ โ thick โ โ
โ โ โฃโโโโโโโโโโโโโโโโโโโโซ โ โฃโโโโโโโโโโณโโโโโโโโโโซ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โฃโโโโโโโโโโโโโโโโโโโโซ โ โฃโโโโโโโโโโโโโโโโโโโโซ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโปโโโโโโโโโโ โ โฃโโโโโโโโโโโโโโโโโโโโซ โ
โ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโโปโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ thick_thin โ โโโโโโโโโโโฌโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ โ _ โ โ โ thick_thin โ โ
โ โ โ โโโโโโโโโโโโโโโโโโโโจ โ โโโโโโโโโโโฌโโโโโโโโโโค โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโผโโโโโโโโโโค โ โ โโโโโโโโโโโโโโโโโโโโจ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโดโโโโโโโโโโ โ โโโโโโโโโโโผโโโโโโโโโโค โ
โ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโโดโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ double โ โโโโโโโโโโโฆโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ โ _ โ โ โ double โ โ
โ โ โ โโโโโโโโโโฌโโโโโโโโโโฃ โ โ โโโโโโโโโโฆโโโโโโโโโโฃ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโฌโโโโโโโโโโฃ โ โ โโโโโโโโโโฌโโโโโโโโโโฃ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโฉโโโโโโโโโโ โ โ โโโโโโโโโโฌโโโโโโโโโโฃ โ
โ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโโฉโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ double_thin โ โโโโโโโโโโโฆโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ โ _ โ โ โ double_thin โ โ
โ โ โโโโโโโโโโโซโโโโโโโโโโข โ โ โโโโโโโโโโฆโโโโโโโโโโฃ โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโฌโโโโโโโโโโฃ โ โโโโโโโโโโโซโโโโโโโโโโข โ
โ โ โ _ โ _ โ โ โ _ โ _ โ โ
โ โ โโโโโโโโโโโฉโโโโโโโโโโ โ โ โโโโโโโโโโฌโโโโโโโโโโฃ โ
โ โ โ โ _ โ _ โ โ
โ โ โ โโโโโโโโโโโฉโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ booktabs โ โโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ _ _ โ booktabs โ
โ โ โโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ _ _ โ _ _ โ
โ โ โโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ _ _ โ _ _ โ
โ โ โโโโโโโโโโโโโโโโโโโ โ โโโโโโโโโโโโโโโโโโโ โ
โ โ โ _ _ โ
โ โ โ โโโโโโโโโโโโโโโโโโโ โ
โโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโผโโโโโโโโโโโโโโโโโโโโโโโโค
โ markdown โ | _ | _ | โ | markdown | โ
โ โ |---------|---------| โ | _ | _ | โ
โ โ | _ | _ | โ |---------|---------| โ
โ โ | _ | _ | โ | _ | _ | โ
โ โ โ | _ | _ | โ
โฐโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโดโโโโโโโโโโโโโโโโโโโโโโโโฏ
Example
>>> from table2string import Themes
>>> table_1 = [("1", "2", "3"), ("qwe", "rty\nuio", "")]
>>> name_1 = "Table Name"
>>> print_table(table_1, theme=Themes.ascii_thin)
+-----+-----+---+
| 1 | 2 | 3 |
+-----+-----+---+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> print_table(table_1, theme=Themes.ascii_thin, name=name_1)
+---------------+
| Table Name |
+-----+-----+---+
| 1 | 2 | 3 |
+-----+-----+---+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> print_table(table_1, theme=Themes.ascii_thin_double)
+-----+-----+---+
| 1 | 2 | 3 |
+=====+=====+===+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> print_table(table_1, theme=Themes.ascii_thin_double, name=name_1)
+---------------+
| Table Name |
+-----+-----+---+
| 1 | 2 | 3 |
+=====+=====+===+
| qwe | rty | |
| | uio | |
+-----+-----+---+
>>> print_table(table_1, theme=Themes.ascii_double)
+=====+=====+===+
โ 1 โ 2 โ 3 โ
+=====+=====+===+
โ qwe โ rty โ โ
โ โ uio โ โ
+=====+=====+===+
>>> print_table(table_1, theme=Themes.ascii_double, name=name_1)
+===============+
โ Table Name โ
+=====+=====+===+
โ 1 โ 2 โ 3 โ
+=====+=====+===+
โ qwe โ rty โ โ
โ โ uio โ โ
+=====+=====+===+
>>> print_table(table_1, theme=Themes.ascii_double_thin)
+=====+=====+===+
โ 1 โ 2 โ 3 โ
+-----+-----+---+
โ qwe โ rty โ โ
โ โ uio โ โ
+=====+=====+===+
>>> print_table(table_1, theme=Themes.ascii_double_thin, name=name_1)
+===============+
โ Table Name โ
+=====+=====+===+
โ 1 โ 2 โ 3 โ
+-----+-----+---+
โ qwe โ rty โ โ
โ โ uio โ โ
+=====+=====+===+
>>> print_table(table_1, theme=Themes.ascii_booktabs)
---------------
1 2 3
===============
qwe rty
uio
---------------
>>> print_table(table_1, theme=Themes.ascii_booktabs, name=name_1)
---------------
Table Name
---------------
1 2 3
===============
qwe rty
uio
---------------
>>> print_table(table_1, theme=Themes.thin)
โโโโโโโฌโโโโโโฌโโโโ
โ 1 โ 2 โ 3 โ
โโโโโโโผโโโโโโผโโโโค
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.thin, name=name_1)
โโโโโโโโโโโโโโโโโ
โ Table Name โ
โโโโโโโฌโโโโโโฌโโโโค
โ 1 โ 2 โ 3 โ
โโโโโโโผโโโโโโผโโโโค
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.thin_thick)
โโโโโโโฌโโโโโโฌโโโโ
โ 1 โ 2 โ 3 โ
โโโโโโโฟโโโโโโฟโโโโฅ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.thin_thick, name=name_1)
โโโโโโโโโโโโโโโโโ
โ Table Name โ
โโโโโโโฌโโโโโโฌโโโโค
โ 1 โ 2 โ 3 โ
โโโโโโโฟโโโโโโฟโโโโฅ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.thin_double)
โโโโโโโฌโโโโโโฌโโโโ
โ 1 โ 2 โ 3 โ
โโโโโโโชโโโโโโชโโโโก
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.thin_double, name=name_1)
โโโโโโโโโโโโโโโโโ
โ Table Name โ
โโโโโโโฌโโโโโโฌโโโโค
โ 1 โ 2 โ 3 โ
โโโโโโโชโโโโโโชโโโโก
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.rounded)
โญโโโโโโฌโโโโโโฌโโโโฎ
โ 1 โ 2 โ 3 โ
โโโโโโโผโโโโโโผโโโโค
โ qwe โ rty โ โ
โ โ uio โ โ
โฐโโโโโโดโโโโโโดโโโโฏ
>>> print_table(table_1, theme=Themes.rounded, name=name_1)
โญโโโโโโโโโโโโโโโโฎ
โ Table Name โ
โโโโโโโฌโโโโโโฌโโโโค
โ 1 โ 2 โ 3 โ
โโโโโโโผโโโโโโผโโโโค
โ qwe โ rty โ โ
โ โ uio โ โ
โฐโโโโโโดโโโโโโดโโโโฏ
>>> print_table(table_1, theme=Themes.rounded_thick)
โญโโโโโโฌโโโโโโฌโโโโฎ
โ 1 โ 2 โ 3 โ
โโโโโโโฟโโโโโโฟโโโโฅ
โ qwe โ rty โ โ
โ โ uio โ โ
โฐโโโโโโดโโโโโโดโโโโฏ
>>> print_table(table_1, theme=Themes.rounded_thick, name=name_1)
โญโโโโโโโโโโโโโโโโฎ
โ Table Name โ
โโโโโโโฌโโโโโโฌโโโโค
โ 1 โ 2 โ 3 โ
โโโโโโโฟโโโโโโฟโโโโฅ
โ qwe โ rty โ โ
โ โ uio โ โ
โฐโโโโโโดโโโโโโดโโโโฏ
>>> print_table(table_1, theme=Themes.rounded_double)
โญโโโโโโฌโโโโโโฌโโโโฎ
โ 1 โ 2 โ 3 โ
โโโโโโโชโโโโโโชโโโโก
โ qwe โ rty โ โ
โ โ uio โ โ
โฐโโโโโโดโโโโโโดโโโโฏ
>>> print_table(table_1, theme=Themes.rounded_double, name=name_1)
โญโโโโโโโโโโโโโโโโฎ
โ Table Name โ
โโโโโโโฌโโโโโโฌโโโโค
โ 1 โ 2 โ 3 โ
โโโโโโโชโโโโโโชโโโโก
โ qwe โ rty โ โ
โ โ uio โ โ
โฐโโโโโโดโโโโโโดโโโโฏ
>>> print_table(table_1, theme=Themes.thick)
โโโโโโโณโโโโโโณโโโโ
โ 1 โ 2 โ 3 โ
โฃโโโโโโโโโโโโโโโโซ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโปโโโโโโปโโโโ
>>> print_table(table_1, theme=Themes.thick, name=name_1)
โโโโโโโโโโโโโโโโโ
โ Table Name โ
โฃโโโโโโณโโโโโโณโโโโซ
โ 1 โ 2 โ 3 โ
โฃโโโโโโโโโโโโโโโโซ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโปโโโโโโปโโโโ
>>> print_table(table_1, theme=Themes.thick_thin)
โโโโโโโฌโโโโโโฌโโโโ
โ 1 โ 2 โ 3 โ
โ โโโโโโโโโโโโโโโโจ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.thick_thin, name=name_1)
โโโโโโโโโโโโโโโโโ
โ Table Name โ
โโโโโโโฌโโโโโโฌโโโโค
โ 1 โ 2 โ 3 โ
โ โโโโโโโโโโโโโโโโจ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโดโโโโโโดโโโโ
>>> print_table(table_1, theme=Themes.double)
โโโโโโโฆโโโโโโฆโโโโ
โ 1 โ 2 โ 3 โ
โ โโโโโโฌโโโโโโฌโโโโฃ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโฉโโโโโโฉโโโโ
>>> print_table(table_1, theme=Themes.double, name=name_1)
โโโโโโโโโโโโโโโโโ
โ Table Name โ
โ โโโโโโฆโโโโโโฆโโโโฃ
โ 1 โ 2 โ 3 โ
โ โโโโโโฌโโโโโโฌโโโโฃ
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโฉโโโโโโฉโโโโ
>>> print_table(table_1, theme=Themes.double_thin)
โโโโโโโฆโโโโโโฆโโโโ
โ 1 โ 2 โ 3 โ
โโโโโโโซโโโโโโซโโโโข
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโฉโโโโโโฉโโโโ
>>> print_table(table_1, theme=Themes.double_thin, name=name_1)
โโโโโโโโโโโโโโโโโ
โ Table Name โ
โ โโโโโโฆโโโโโโฆโโโโฃ
โ 1 โ 2 โ 3 โ
โโโโโโโซโโโโโโซโโโโข
โ qwe โ rty โ โ
โ โ uio โ โ
โโโโโโโฉโโโโโโฉโโโโ
>>> print_table(table_1, theme=Themes.booktabs)
โโโโโโโโโโโโโโโ
1 2 3
โโโโโโโโโโโโโโโ
qwe rty
uio
โโโโโโโโโโโโโโโ
>>> print_table(table_1, theme=Themes.booktabs, name=name_1)
โโโโโโโโโโโโโโโ
Table Name
โโโโโโโโโโโโโโโ
1 2 3
โโโโโโโโโโโโโโโ
qwe rty
uio
โโโโโโโโโโโโโโโ
>>> print_table(table_1, theme=Themes.markdown)
| 1 | 2 | 3 |
|-----|-----|---|
| qwe | rty | |
| | uio | |
>>> print_table(table_1, theme=Themes.markdown, name=name_1)
| Table Name |
| 1 | 2 | 3 |
|-----|-----|---|
| qwe | rty | |
| | uio | |
Example
from prettytable import PrettyTable
from table2string import Table
names = ("plain text", "emoji")
table = [
(
"text\ntext",
"๐จโ๐ฉโ๐งโ๐ฆ๐จโ๐ฉโ๐ฆโ๐ฆ๐จโ๐ฉโ๐งโ๐ง\n"
"๐จโ๐จโ๐งโ๐ฆ๐จโ๐จโ๐งโ๐ง๐จโ๐ฉโ๐ง๐ฉโโค๏ธโ๐จ\n"
"๐จโโค๏ธโ๐จ๐ฏ๐ฉโ๐ฆผ๐ญ๐จโ๐ฉโ๐งโ๐ฆ\n"
"๐จโ๐จโ๐งโ๐ฆ๐จโ๐จโ๐ฆ๐ฉโ๐ฉโ๐ง\n"
"๐จโ๐จโ๐งโ๐ง๐จโ๐ฉโ๐ฆโ๐ฆ",
),
]
t = PrettyTable(title="prettytable", field_names=names, align="c")
t.add_rows(table)
print(t)
t = Table(table, name="table2string", column_names=names)
t.print(align="^", sep=(1,))