Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Create Footer for Python PrettyTable

I'm looking to add a footer to my PrettyTable, totalling the data stored in the rows above. I've created a count in the script, but I'd like to add this into the table.

The code I have to create the table below is as follows (.add_row is in a loop):

outTbl = PrettyTable(["Projects", "Number"])
outTbl.add_row([eachProj, count])

...which generates a table looking like this:

+--------------------------+-----------+
|        Projects          |   Number  |
+--------------------------+-----------+
|        Project A         |     5     |
|        Project B         |     9     |
|        Project C         |     8     |
|        Project D         |     2     |
+--------------------------+-----------+

...but I'm looking for the functionality to create the above table with a summary footer at the bottom:

+--------------------------+-----------+
|        Projects          |   Number  |
+--------------------------+-----------+
|        Project A         |     5     |
|        Project B         |     9     |
|        Project C         |     8     |
|        Project D         |     2     |
+--------------------------+-----------+
|          Total           |     24    |
+--------------------------+-----------+

I've searched the module docs online: PrettyTable tutorial, Google prettytable - Tutorial and can't see any reference to a footer, which I find surprising given header is one. Can this be done in PrettyTable, or is there another Python module with this functionality anyone can recommend?

like image 241
DeltaKilo Avatar asked Mar 18 '26 14:03

DeltaKilo


2 Answers

You can use texttable with small hack around it:

import texttable

table = texttable.Texttable()
table.add_rows([['Projects', 'Number'],
                ['Project A\nProject B\nProject C\nProject D', '5\n9\n8\n2'],
                ['Total', 24]])
print(table.draw())

Output:

+-----------+--------+
| Projects  | Number |
+===========+========+
| Project A | 5      |
| Project B | 9      |
| Project C | 8      |
| Project D | 2      |
+-----------+--------+
| Total     | 24     |
+-----------+--------+
like image 183
Alderven Avatar answered Mar 21 '26 03:03

Alderven


prettytable has supported footer by passing divider=True to add_row().

Here is an example from https://pypi.org/project/prettytable/

code:

x = PrettyTable()
x.field_names = ["City name", "Area", "Population", "Annual Rainfall"]
x.add_row(["Adelaide", 1295, 1158259, 600.5])
x.add_row(["Brisbane", 5905, 1857594, 1146.4])
x.add_row(["Darwin", 112, 120900, 1714.7])
x.add_row(["Hobart", 1357, 205556, 619.5], divider=True)
x.add_row(["Melbourne", 1566, 3806092, 646.9])
x.add_row(["Perth", 5386, 1554769, 869.4])
x.add_row(["Sydney", 2058, 4336374, 1214.8])

output:

+-----------+------+------------+-----------------+
| City name | Area | Population | Annual Rainfall |
+-----------+------+------------+-----------------+
|  Adelaide | 1295 |  1158259   |      600.5      |
|  Brisbane | 5905 |  1857594   |      1146.4     |
|   Darwin  | 112  |   120900   |      1714.7     |
|   Hobart  | 1357 |   205556   |      619.5      |
+-----------+------+------------+-----------------+
| Melbourne | 1566 |  3806092   |      646.9      |
|   Perth   | 5386 |  1554769   |      869.4      |
|   Sydney  | 2058 |  4336374   |      1214.8     |
+-----------+------+------------+-----------------+
like image 33
hourouer Avatar answered Mar 21 '26 04:03

hourouer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!