einmalundweg
einmalundweg

Reputation: 103

How can I define the width of a column (PrettyTable, Python)?

So, I got a table (PrettyTable) and I want to make them as wide as I want. For example, the first column should have a width of 5px, the second of 18px, and so on... How can I do this?

Upvotes: 8

Views: 10083

Answers (2)

Satyam Jain
Satyam Jain

Reputation: 11

In PrettyTable:

_max_width : define a dict of column width in maximum characters.

_min_width : define a dict of column width in minimum characters.

To have a dynamic column width and yet define a max column width use:

table._max_width = {"Column 1" : 50, "Column 2" : 25}

Specify the field names and the max characters it can fit; exceeding characters wrap onto the next line.

Fields not listed will still auto adjust to long texts.

To fix the width of the column using PrettyTable, both dicts _min_width and _max_width would be same.

table._min_width = {"Column 1" : 50, "Column 2" : 25}
table._max_width = {"Column 1" : 50, "Column 2" : 25}

Upvotes: 1

tapyokka
tapyokka

Reputation: 121

PrettyTable's _max_width can define column/field width in maximum characters instead.

table._max_width = {"Field 1" : 50, "Field 3" : 25}

Specify the field names and the max characters it can fit; exceeding characters wrap onto the next line.

Fields not listed will still auto adjust to long texts.

Upvotes: 12

Related Questions