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.0.tar.gz
(14.7 kB
view hashes)
Built Distribution
Close
Hashes for table2string-1.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed37665822fd7bda691877074b63854b1f704513d8572d387b7c31a382c13208 |
|
MD5 | 62ac129b928bdc67ce7240999b7bbf3f |
|
BLAKE2b-256 | 8b471dddf4d5aa123e6929349cfb1ca9c7af70e86c1ae481dc031c13f2c72145 |