1

I'm using the python module BeautifulTable to emit a table to a console. It's wrapping some of the headers and contents and I'd like to set the cell width so it doesn't do that.

I've tried setting the widths via table.column_widths (as per the docs) but that doesn't seem to have any effect.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
Michael Ray Lovett
  • 6,668
  • 7
  • 27
  • 36

4 Answers4

3

From the docs:

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.

Thus column_widths only handles the max number of characters in a column. For handling overflowing text width_exceed_policy has to be used.

table.width_exceed_policy = BeautifulTable.WEP_ELLIPSIS

From the docs it can be one of the following:

  1. BeautifulTable.WEP_WRAP : An item is wrapped so every line fits within it’s column width.
  2. BeautifulTable.WEP_STRIP : An item is stripped to fit in it’s column.
  3. BeautifulTable.WEP_ELLIPSIS : An item is stripped to fit in it’s column and appended with …(Ellipsis).
nishant
  • 896
  • 1
  • 8
  • 27
3

You can specify width using table.columns.width, e.g.

table.columns.width = 30

or

table.columns.width = [20, 30, 40]

If the table contains dynamic data, it may be better to increase maxwidth from the default 80:

table = BeautifulTable(maxwidth=120)

You could also wrap the data yourself with the desired width before putting it to a column. The textwrap stdlib module may be useful for that. For JSON data it may help to pretty-print it with json.dumps(), e.g.:

json.dumps(data, indent=2)
Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378
2

If you are using BeautifulTable 1.0.0 you have the following syntax:

table.columns.width = [12,5,8,10,6,33]
print(table)

while the older syntax table.column_widths = [12,5,8,10,6,33] tends to be automatically overriden when print(table) is called.

From the official release notes:

Updated behaviour of BeautifulTable.columns.width (earlier BeautifulTable.column_widths). It no longer overrides user specified widths by default. You can reset it to default by setting it to "auto"

Louis Gagnon
  • 129
  • 1
  • 3
1

Per the docs and the change log for v0.8.0, controlling column width can be handled by setting the column_widths attribute:

>>> table.column_widths = 5
>>> print(table.column_widths)
PositiveIntegerMetaData<5, 5, 5, 5, 5, 5, 5>

>>> table.column_widths = [10, 20, 5, 7, 10, 10, 5]
>>> print(table.column_widths)
PositiveIntegerMetaData<10, 20, 5, 7, 10, 10, 5>

Note that the most recent change log also says it dropped support for Python v3.3 I have only tested this in Python v3.7.5 where, as soon as the table is printed again, these values are overwritten.

If you're using v3.3+ and you don't want to truncate or wrap your headers/fields then the only 'work around' that I have found is to set the max_table_width to a value large enough to accommodate all your row contents + padding + borders:

>>> table.max_table_width = 150

Of course, this has its obvious limits.

Clayin
  • 19
  • 2