Anish
Anish

Reputation: 137

Shrink tabulate Table Size in python?

I want to send this table made using tabulate,

In a telegram bot

+--------+----------------+---------+---------------+------+
| Course | course         | Credits | Credits       |      |
|  code  | name           |  Reg.   |  Earned Grade |      |
+--------+----------------+---------+---------------+------+
| CY22   | ENGINEERING    | 3.00    | 3.00          | A    |
|        | CHEMISTRY      |         |               |      |
+--------+----------------+---------+---------------+------+
| EE25   | BASIC          | 3.00    | 3.00          | B    |
|        | ELECTRICAL     |         |               |      |
|        | ENGINEERING    |         |               |      |
+--------+----------------+---------+---------------+------+
| MA21   | ENGINEERING    | 4.00    | 4.00          | B    |
|        | MATHEMATICS-II |         |               |      |
+--------+----------------+---------+---------------+------+
| HS24   | PROFESSIONAL   | 2.00    | 2.00          | A    |
|        | COMMUNICATION  |         |               |      |
|        | AND            |         |               |      |
|        | LIFE           |         |               |      |
|        | SKILLS         |         |               |      |
+--------+----------------+---------+---------------+------+
| HS23   | CONSTITUTION   | 0.00    | 0.00          | Pass |
|        | OF             |         |               |      |
|        | INDIA          |         |               |      |
+--------+----------------+---------+---------------+------+
| CS26   | FUNDAMENTALS   | 2.00    | 2.00          | A    |
|        | OF             |         |               |      |
|        | COMPUTING      |         |               |      |
+--------+----------------+---------+---------------+------+
| CSL28  | FUNDAMENTALS   | 2.00    | 2.00          | A    |
|        | OF             |         |               |      |
|        | COMPUTING      |         |               |      |
|        | &              |         |               |      |
|        | C              |         |               |      |
|        | PROGRAMMING    |         |               |      |
|        | LABORATORY     |         |               |      |
+--------+----------------+---------+---------------+------+
| CYL27  | ENGINEERING    | 1.00    | 1.00          | B    |
|        | CHEMISTRY      |         |               |      |
|        | LAB            |         |               |      |
+--------+----------------+---------+---------------+------+
| AL21   | ENGINEERING    | 1.00    | 1.00          | A    |
|        | DESIGN         |         |               |      |
+--------+----------------+---------+---------------+------+
| ME29   | COMPUTER       | 2.00    | 2.00          | A    |
|        | AIDED          |         |               |      |
|        | ENGINEERING    |         |               |      |
|        | DRAWING        |         |               |      |
+--------+----------------+---------+---------------+------+

But When the bot sends this table in telegram it would have lost its format and will become something like this

enter image description here

How to format the table? I got same result with pretty table also.

Upvotes: 0

Views: 2065

Answers (1)

Rishabh Kumar
Rishabh Kumar

Reputation: 2430

By my observation, the text is formatted just that the font used in telegram have different width for different characters.

Add backtick ` at the start and end of every string. In telegram text enclosed in backticks are interpreted as monospace.

So suppose your text output from tabulate is stored in txt variable.

   finaltext = '`'+txt.replace('\n','`\n`')+'`'

This will add backticks at start and end of everyline.

Upvotes: 1

Related Questions