1

I have the following code for pretty table which goes like this:

from prettytable import PrettyTable

myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
myTable.title = 'Big Bang Theory'
  
# Add rows
myTable.add_row(["Leanord", "X", "B", "91.2 %"])
myTable.add_row(["Penny", "X", "C", "63.5 %"])
myTable.add_row(["Howard", "X", "A", "90.23 %"])
myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
myTable.add_row(["Raj", "X", "B", "88.1 %"])
myTable.add_row(["Amy", "X", "B", "95.0 %"])
print(myTable)

This produces the following table:

+---------------------------------------------+
|               Big Bang Theory               |
+--------------+-------+---------+------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

I was wondering if it possible for the following layout:

+--------------(Big Bang Theory)--------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

Kindly please advise if such a layout is possible

Edit 1

I added this:

table_txt = myTable.get_string()

enter image description here

table_txt = table_txt.replace("+---------------------------------------------+\n|               Big Bang Theory               |\n+--------------+-------+---------+------------+", "+--------------(Big Bang Theory)--------------+")

print(table_txt)
+--------------(Big Bang Theory)--------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

This approach assumes that table width is static thus if the width changes, the junction of the first line does not align with the rest of the table.

Slartibartfast
  • 1,058
  • 4
  • 26
  • 60
  • You can export the table in a string and do some custom formatting, based on table width and title lenght. – Jakob Oct 25 '21 at 13:54
  • @Jakobヤコブ Do you mean something like this: `table_txt = myTable.get_string()` and `table_txt.replace("+---------------------------------------------+\n| Big Bang Theory |\n+--------------+-------+---------+------------+", "+--------------(Big Bang Theory)--------------+")` – Slartibartfast Oct 25 '21 at 14:15
  • 1
    Basically yes. But if you also want to use a different title it needs to be much more dynamic. – Jakob Oct 25 '21 at 14:18
  • @Jakobヤコブ So title should be saved in a variable using regex to find center and overwrite character? – Slartibartfast Oct 25 '21 at 14:19
  • 1
    I will write a quick answer (comment is to small). – Jakob Oct 25 '21 at 14:23

2 Answers2

1
  1. Export your table to a string, and keep the title in the variable as you already do.
  2. Calculate the width of the table. This can easily be done by splitting the string at \n and getting the length of any string in the split array.
  3. Remove the first 2 rows of the table. You can use split function or do some regex.
  4. Write a function to build a string of - with a centered title based on table width.
  5. Print the string.
Jakob
  • 1,858
  • 2
  • 15
  • 26
1

You can do something like this

#! /usr/bin/env python3
from prettytable import PrettyTable

myTable = PrettyTable(["Student Name", "Class", "Section", "Percentage"])
title = 'Big Bang Theory'
  
# Add rows
myTable.add_row(["Leanord", "X", "B", "91.2 %"])
myTable.add_row(["Penny", "X", "C", "63.5 %"])
myTable.add_row(["Howard", "X", "A", "90.23 %"])
myTable.add_row(["Bernadette", "X", "D", "92.7 %"])
myTable.add_row(["Sheldon", "X", "A", "98.2 %"])
myTable.add_row(["Raj", "X", "B", "88.1 %"])
myTable.add_row(["Amy", "X", "B", "95.0 %"])
for n, l in enumerate(myTable.get_string().split('\n')):
    if n == 0:
        l = f"+{f'({title})'.center(len(l)-2, '-')}+"
    print(l)

It is enumerating the lines of the table as a string and for the first line, center the title and replace it.

+--------------(Big Bang Theory)--------------+
| Student Name | Class | Section | Percentage |
+--------------+-------+---------+------------+
|   Leanord    |   X   |    B    |   91.2 %   |
|    Penny     |   X   |    C    |   63.5 %   |
|    Howard    |   X   |    A    |  90.23 %   |
|  Bernadette  |   X   |    D    |   92.7 %   |
|   Sheldon    |   X   |    A    |   98.2 %   |
|     Raj      |   X   |    B    |   88.1 %   |
|     Amy      |   X   |    B    |   95.0 %   |
+--------------+-------+---------+------------+

Works for any width.

edit

Not sure if I understood your comment, but I think you mean to have the table as string and not print it, then do this

table_txt = ''
for n, l in enumerate(myTable.get_string().split('\n')):
    if n == 0:
        l = f"+{f'({title})'.center(len(l)-2, '-')}+"
    table_txt = f'{table_txt}{l}\n'

You may find other ways of concatenating strings here but you should not have performance problems with these kind of tables.

Diego Torres Milano
  • 65,697
  • 9
  • 111
  • 134
  • Thanks for your answer. Quick question, I am not able to print the table if I tab `print(l)` left. `myTable.get_string()` is variable `table_txt` for me and the modification has to be saved on `table_txt`. Could you please advise! – Slartibartfast Apr 11 '22 at 17:58