A library to convert tables to string with full support for line breaks and formatting
Project description
table2string
Convert table to string
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.
Install
PyPI
pip install -U table2string
GitHub
pip install -U git+https://github.com/EgorKhabarov/table2string.git@master
Usage example
>>> 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"), end="")
+---------------+
| Table Name |
+-----+-----+---+
| 1 | 2 | 3 |
+-----+-----+---+
| qwe | rty | |
| | uio | |
+-----+-----+---+
Custom width and height settings
>>> # 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…|
+---+
Text alignment
>>> print_table([("1", "example")], max_width=25, name="Table Name", align="<", name_align="<")
+-----------------------+
| Table Name |
+-----------+-----------+
| 1 | example |
+-----------+-----------+
>>> print_table([("1", "example")], max_width=25, name="Table Name", align=">", name_align=">")
+-----------------------+
| Table Name |
+-----------+-----------+
| 1 | example |
+-----------+-----------+
>>> print_table([("1", "example")], max_width=25, name="Table Name", align="^", name_align="^")
+-----------------------+
| Table Name |
+-----------+-----------+
| 1 | example |
+-----------+-----------+
>>> print_table([("1", "example")], max_width=25, name="Table Name", align="*", name_align="*")
+-----------------------+
| Table Name |
+-----------+-----------+
| 1 | example |
+-----------+-----------+
>>> print_table([("1", "example")], max_width=25, name="Table Name")
+-----------------------+
| Table Name |
+-----------+-----------+
| 1 | example |
+-----------+-----------+
Separator settings
>>> table_1 = [("qwe", "rty\nuio"), ("123456\n\n789000", "example")]
>>> kwargs = {
... "max_width": (3, 4),
... "max_height": 4,
... "line_break_symbol": "/",
... "cell_break_symbol": "…",
... }
>>> 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 |
+---+---+
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
table2string-1.1.1.tar.gz
(14.7 kB
view hashes)
Built Distribution
Close
Hashes for table2string-1.1.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f6c1061cb3dbbab59da28e0b48af04a1cb3bea26df72883a61e7988880f1532d |
|
MD5 | 9cbc15da3d347bd5c17080f6ab1bc436 |
|
BLAKE2b-256 | cfc61881c1cec6e1e4f1996a880ca33359baebd8260ca7a1e7058602102378bb |