0

I am using prettytable to generate tables. But when the feild name is too long, it won't switch lines. The example code is like below:

import prettytable

x = prettytable.PrettyTable()
x.max_table_width = 50
x.hrules = prettytable.ALL
x.vrules = prettytable.ALL

x.add_column("Field 1 is too long", ['Cell data also too long', 1])
x.add_column("Field 2 is too long", ['Cell data also too long', 2])
x.add_column("Field 3 is too long", ['Cell data also too long', 3])
x.add_column("Field 4 is too long", ['Cell data also too long', 4])
print(x.get_string())

The result is:

+-------------+-------------+-------------+-------------+
| Field 1 is  | Field 2 is  | Field 3 is  | Field 4 is  |
+-------------+-------------+-------------+-------------+
|  Cell data  |  Cell data  |  Cell data  |  Cell data  |
|   also too  |   also too  |   also too  |   also too  |
|     long    |     long    |     long    |     long    |
+-------------+-------------+-------------+-------------+
|      1      |      2      |      3      |      4      |
+-------------+-------------+-------------+-------------+

The field name is incomplete, while the cell data is complete. How to make the field name auto switch lines? Thanks!

ToughMind
  • 987
  • 1
  • 10
  • 28

1 Answers1

0

seen it now and you have probably already solved it. I post it here for any other people looking for the same answer. Add to the table after

tab = PrettyTable()
tab.header = False

you should solve it like this. Bye

Prectux
  • 1
  • 3