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. this is ignored when manually setting the width of the columns. if this value is too low with respect to the number of columns and width of padding, the resulting table may override it(default 80).
  • default_alignment (int, optional) – Default alignment for new columns(default beautifultable.ALIGN_CENTER).
  • default_padding (int, optional) – Default width of the left and right padding for new columns(default 1).
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_separator_char

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

row_separator_char

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

column_separator_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. (DEPRECATED).

intersect_top_left

str – Left most character of the top border.

intersect_top_mid

str – Intersection character for top border.

intersect_top_right

str – Right most character of the top border.

intersect_header_left

str – Left most character of the header separator.

intersect_header_mid

str – Intersection character for header separator.

intersect_header_right

str – Right most character of the header separator.

intersect_row_left

str – Left most character of the row separator.

intersect_row_mid

str – Intersection character for row separator.

intersect_row_right

str – Right most character of the row separator.

intersect_bottom_left

str – Left most character of the bottom border.

intersect_bottom_mid

str – Intersection character for bottom border.

intersect_bottom_right

str – Right most character of the bottom border.

numeric_precision

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

serialno

bool – Whether automatically generated serial number should be printed for each row(Default False).

serialno_header

str – The header of the autogenerated serial number column. This value is only used if serialno is True(Default SN).

detect_numerics

bool – Whether numeric strings should be automatically detected(Default True).

column_count

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

intersection_char

Character used to draw intersection of perpendicular lines.

Disabling it just draws the horizontal line char in it’s place. This attribute is deprecated. Use specific intersect_*_* attribute.

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 how exceeding column width should be handled.

It can be one of the following:

Option Meaning
beautifulbable.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.

max_table_width

get/set the maximum width of the table.

The width of the table is guaranteed to not exceed this value. If it is not possible to print a given table with the width provided, this value will automatically adjust.

set_style(style)[source]

Set the style of the table from a predefined set of styles.

Parameters:style (Style) –

It can be one of the following:

  • beautifulTable.STYLE_DEFAULT
  • beautifultable.STYLE_NONE
  • beautifulTable.STYLE_DOTTED
  • beautifulTable.STYLE_MYSQL
  • beautifulTable.STYLE_SEPARATED
  • beautifulTable.STYLE_COMPACT
  • beautifulTable.STYLE_MARKDOWN
  • beautifulTable.STYLE_RESTRUCTURED_TEXT
  • beautifultable.STYLE_BOX
  • beautifultable.STYLE_BOX_DOUBLED
  • beautifultable.STYLE_BOX_ROUNDED
  • beautifultable.STYLE_GRID
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, reverse=False)[source]

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

Parameters:
  • key (int, str) – index or header of the column. Normal list rules apply.
  • reverse (bool) – If True then table is sorted as if each comparison was reversed.
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_metadata=False)[source]

Clear the contents of the table.

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

Parameters:clear_metadata (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_separator()[source]

Get the Header separator of table.

Column width should be set prior to calling this method.

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

Get the Row separator of table.

Column width should be set prior to calling this method.

Returns:String which will be printed as Row separator 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 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_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