Skip to main content

terminaltables

Easily draw tables in terminal/console applications from a list of lists of strings. Supports multi-line rows.

terminaltables is supported on Python 2.6, 2.7, 3.3, and 3.4.

Build Status Coverage Status Latest Version Downloads Download format License

Quickstart

Install:

pip install terminaltables

Example Implementations

Example Scripts Screenshot

Source code for examples: example1.py, example2.py, and example3.py

Usage

The below usage information is for AsciiTable which uses simple ASCII characters for the table (e.g. -+|). Use UnixTable for box drawing characters instead. UnixTable has the same methods and properties as AsciiTable.

Simple Usage

from terminaltables import AsciiTable
table_data = [
    ['Heading1', 'Heading2'],
    ['row1 column1', 'row1 column2'],
    ['row2 column1', 'row2 column2']
]
table = AsciiTable(table_data)
print table.table
+--------------+--------------+
| Heading1     | Heading2     |
+--------------+--------------+
| row1 column1 | row1 column2 |
| row2 column1 | row2 column2 |
+--------------+--------------+

table_data is a list of lists of strings. The outer list represents the whole table, while the inner lists represents rows. Each row-list holds strings which are the cells of that row.

The first row can be though of the heading, but it doesn’t have to be. You can turn off the heading separator (the only thing that makes the first row a “heading” row) by setting table.inner_heading_row_border = False.

table.inner_heading_row_border = False
print table.table
+--------------+--------------+
| Heading1     | Heading2     |
| row1 column1 | row1 column2 |
| row2 column1 | row2 column2 |
+--------------+--------------+

If you want to add colors or bold the heading row, you’ll have to do that yourself. Keep in mind that terminaltables relies on len() and other methods for calculating table borders. I suggest looking at colorclass for supporting colors in terminaltables since it handles color string lengths correctly.

Class Attributes

You can instantiate with AsciiTable(table_data) or AsciiTable(table_data, 'Table Title'). These are available after instantiating AsciiTable or UnixTable.

Name

Description/Notes

table_data

List of list of strings. Same object passed to __init__().

title

Table title string. Default is None for no title.

inner_column_border

Default is True. Separates columns.

inner_heading_row_border

Default is True. This is what makes the first row a “header row”.

inner_row_border

Default is False. This adds lines between rows.

justify_columns

Dictionary. Keys are column numbers (0 base), values are ‘left’, ‘right’, or ‘center’.

outer_border

Default is True. Toggles the top, bottom, left, and right table borders.

padding_left

Default is 1. Number of spaces to add to the left of the cell.

padding_right

Default is 1. Number of spaces to add to the right of the cell.

Class Methods

These are regular methods available in either class.

Name

Description/Notes

column_max_width

Takes one argument, column number (0 base). Returns The maximum size it will fit in the terminal without breaking the table. Takes other columns into account.

Class Properties

These are read-only properties after you instantiate either class. They are “real-time”. You do not have to re-instantiate if you change any of the class attributes, including table_data.

Name

Description/Notes

column_widths

Returns a list with the current column widths (one int per column) without padding.

padded_table_data

Returns the padding table data. With spaces and newlines. Does not include borders.

table

Returns a large string, the whole table. This may be printed to the terminal.

Metadata

Release files for terminaltables 1.0.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for terminaltables 1.0.1
File Size Uploaded
terminaltables-1.0.1.tar.gz 7.3 kB Details

Release files / terminaltables-1.0.1.tar.gz

Download URL terminaltables-1.0.1.tar.gz
Size 7.3 kB
Tags Source
SHA-256 checksum
How to use checksums
5548ac567d38d6ac88a5e0fec2d95f646249f37e1ef8fd2d17f8fcaefc6cf592
BLAKE2b-256 checksum
How to use checksums
9765858bc3ea6cc60edc959ce427a94227932b5d9a95b0bce82f16071419885c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

3.1.9

2 release files

3.1.8

2 release files

3.1.7

2 release files

3.1.6

2 release files

3.1.5

1 release file

3.1.0

1 release file

3.0.0

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.2.1

1 release file

1.2.0

1 release file

1.1.1

1 release file

1.1.0

1 release file

1.0.2

1 release file

This release

1.0.1 This release

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page