beautifultable package

Module contents

class beautifultable.BeautifulTable(max_width=80, default_alignment=ALIGN_CENTER, default_padding=1)[source]

Bases: object

Utility Class to print data in tabular format to terminal.

The instance attributes can be used to customize the look of the table. To disable a behaviour, just set its corresponding attribute to an empty string. For example, if Top border should not be drawn, set top_border_char to ‘’.

Parameters:
  • max_width (int, optional) – maximum width of the table in number of characters.
  • default_alignment (int, optional) – Default alignment for new columns.
  • default_padding (int, optional) – Default width of the left and right padding for new columns.
sign_mode
width_exceed_policy
default_alignment
default_padding
column_widths
column_headers
column_alignments
left_padding_widths
right_padding_widths
left_border_char

str – Character used to draw the left border.

right_border_char

str – Character used to draw the right border.

top_border_char

str – Character used to draw the top border.

bottom_border_char

str – Character used to draw the bottom border.

header_seperator_char

str – Character used to draw the line seperating Header from data.

row_seperator_char

str – Character used to draw the line seperating two rows.

column_seperator_char

str – Character used to draw the line seperating two columns.

intersection_char

str – Character used to draw intersection of a vertical and horizontal line. Disabling it just draws the horizontal line char in it’s place.

numeric_precision

int – All float values will have maximum number of digits after the decimal, capped by this value(Default 3).

column_count

Get the number of columns in the table(read only)

sign_mode

Attribute to control how signs are displayed for numerical data.

It can be one of the following:

Option Meaning
BeautifulTable.SM_PLUS A sign should be used for both +ve and -ve numbers.
BeautifulTable.SM_MINUS A sign should only be used for -ve numbers.
BeautifulTable.SM_SPACE A leading space should be used for +ve numbers and a minus sign for -ve numbers.
width_exceed_policy

Attribute to control the behaviour of table when items exceed the column width.

It can be one of the following:

Option Meaning
BeautifulTable.WEP_WRAP An item is wrapped so every line fits within it’s column width.
BeautifulTable.WEP_STRIP An item is stripped to fit in it’s column.
BeautifulTable.WEP_ELLIPSIS An item is stripped to fit in it’s column and appended with …(Ellipsis).
default_alignment

Attribute to control the alignment of newly created columns.

It can be one of the following:

Option Meaning
BeautifulTable.ALIGN_LEFT New columns are left aligned.
BeautifulTable.ALIGN_CENTER New columns are center aligned.
BeautifulTable.ALIGN_RIGHT New columns are right aligned.
default_padding

Initial value for Left and Right padding widths for new columns.

column_widths

get/set width for the columns of the table.

Width of the column specifies the max number of characters a column can contain. Larger characters are handled according to the value of width_exceed_policy.

column_headers

get/set titles for the columns of the table.

It can be any iterable having all memebers an instance of str.

column_alignments

get/set alignment of the columns of the table.

It can be any iterable containing only the following:

  • BeautifulTable.ALIGN_LEFT
  • BeautifulTable.ALIGN_CENTER
  • BeautifulTable.ALIGN_RIGHT
left_padding_widths

get/set width for left padding of the columns of the table.

Left Width of the padding specifies the number of characters on the left of a column reserved for padding. By Default It is 1.

right_padding_widths

get/set width for right padding of the columns of the table.

Right Width of the padding specifies the number of characters on the rigth of a column reserved for padding. By default It is 1.

auto_calculate_width()[source]

Calculate width of column automatically based on data.

set_padding_widths(pad_width)[source]

Set width for left and rigth padding of the columns of the table.

Parameters:pad_width (array_like) – pad widths for the columns.
sort(key)[source]

Stable sort of the table IN-PLACE with respect to a column.

Parameters:index – index of the column. Normal list rules apply.
copy()[source]

Return a shallow copy of the table.

Returns:shallow copy of the BeautifulTable instance.
Return type:BeautifulTable
get_column_header(index)[source]

Get header of a column from it’s index.

Parameters:index (int) – Normal list rules apply.
get_column_index(header)[source]

Get index of a column from it’s header.

Parameters:header (str) – header of the column.
Raises:ValueError: – If no column could be found corresponding to header.
get_column(key)[source]

Return an iterator to a column.

Parameters:key (int, str) – index of the column, or the header of the column. If index is specified, then normal list rules apply.
Raises:TypeError: – If key is not of type int, or str.
Returns:Iterator to the specified column.
Return type:iter
reverse()[source]

Reverse the table row-wise IN PLACE.

pop_row(index=-1)[source]

Remove and return row at index (default last).

Parameters:index (int) – index of the row. Normal list rules apply.
pop_column(index=-1)[source]

Remove and return row at index (default last).

Parameters:

index (int, str) – index of the column, or the header of the column. If index is specified, then normal list rules apply.

Raises:
  • TypeError: – If index is not an instance of int, or str.
  • IndexError: – If Table is empty.
insert_row(index, row)[source]

Insert a row before index in the table.

Parameters:
  • index (int) – List index rules apply
  • row (iterable) – Any iterable of appropriate length.
Raises:
  • TypeError: – If row is not an iterable.
  • ValueError: – If size of row is inconsistent with the current number of columns.
append_row(row)[source]

Append a row to end of the table.

Parameters:row (iterable) – Any iterable of appropriate length.
update_row(key, value)[source]

Update a column named header in the table.

If length of column is smaller than number of rows, lets say k, only the first k values in the column is updated.

Parameters:
  • key (int or slice) – index of the row, or a slice object.
  • value (iterable) – If an index is specified, value should be an iterable of appropriate length. Instead if a slice object is passed as key, value should be an iterable of rows.
Raises:
  • IndexError: – If index specified is out of range.
  • TypeError: – If value is of incorrect type.
  • ValueError: – If length of row does not matches number of columns.
update_column(header, column)[source]

Update a column named header in the table.

If length of column is smaller than number of rows, lets say k, only the first k values in the column is updated.

Parameters:
  • header (str) – Header of the column
  • column (iterable) – Any iterable of appropriate length.
Raises:
  • TypeError: – If length of column is shorter than number of rows.
  • ValueError: – If no column exists with title header.
insert_column(index, header, column)[source]

Insert a column before index in the table.

If length of column is bigger than number of rows, lets say k, only the first k values of column is considered. If column is shorter than ‘k’, ValueError is raised.

Note that Table remains in consistent state even if column is too short. Any changes made by this method is rolled back before raising the exception.

Parameters:
  • index (int) – List index rules apply.
  • header (str) – Title of the column.
  • column (iterable) – Any iterable of appropriate length.
Raises:
  • TypeError: – If header is not of type str.
  • ValueError: – If length of column is shorter than number of rows.
append_column(header, column)[source]

Append a column to end of the table.

Parameters:
  • header (str) – Title of the column
  • column (iterable) – Any iterable of appropriate length.
clear(clear_column_properties=False)[source]

Clear the contents of the table.

Clear all rows of the table, and if specified clears all column specific data.

Parameters:clear_column_properties (bool, optional) – If it is true(default False), all metadata of columns such as their alignment, padding, width, etc. are also cleared and number of columns is set to 0.
get_top_border()[source]

Get the Top border of table.

Column width should be set prior to calling this method.

Returns:String which will be printed as the Top border of the table.
Return type:str
get_header_seperator()[source]

Get the Header seperator of table.

Column width should be set prior to calling this method.

Returns:String which will be printed as Header seperator of the table.
Return type:str
get_row_seperator()[source]

Get the Row seperator of table.

Column width should be set prior to calling this method.

Returns:String which will be printed as Row seperator of the table.
Return type:str
get_bottom_border()[source]

Get the Bottom border of table.

Column width should be set prior to calling this method.

Returns:String which will be printed as the Bottom border of the table.
Return type:str
get_table_width()[source]

Get the width of the table as number of characters.

Column width should be set prior to calling this method.

Returns:Width of the table as number of characters.
Return type:int
get_number_of_columns()[source]

Get the current number of columns.

Returns:Current number of columns in the Table.
Return type:int
get_string(recalculate_width=True)[source]

Get the table as a String.

Parameters:recalculate_width (bool, optional) – If width for each column should be recalculated(default True). Note that width is always calculated if it wasn’t set explicitly when this method is called for the first time , regardless of the value of recalculate_width.
Returns:Table as a string.
Return type:str