3

I am currently working on my School project and in that I created a module that can input a nested list and then converts into a table (or precisely a string that looks like a table).

#Working of program
nested_list = nested_list = [['This', 'is' , 'a' , 'demo'], ['Hello', 'How', 'are'], ['You']]

#Creating table (doc string)
#the program processes the nested list to generate a doc string like the following
table = """
+-------------+-------------+-------------+-------------+
|    This     |     is      |      a      |    demo     |
+-------------+-------------+-------------+-------------+
|    Hello    |     How     |     are     |      -      |
+-------------+-------------+-------------+-------------+
|     You     |      -      |      -      |      -      |
+-------------+-------------+-------------+-------------+ """
#when printed it looks like a table.

The problem is that it works with monospace fonts only and it looks horrible when the string is wrapped. The only way I can fix it is by explicitly configuring the font, height, width and color of font of command prompt window via the program itself.

How can I do that in Python 3.7.4 (version of python I am using currently)?

I am currently using Windows 10 and I have to run the program on Command Prompt(for sure !).

Tushar
  • 167
  • 7
  • Can you clarify this? Are you just creating a table that has a certain format when it prints to the console? There will always be some issues if you depend on that. Might be better to have your code output an html table so the formatting is more controlled. – bart cubrich Nov 06 '20 at 00:25
  • @ppwater its quite large so i didn't include it here – Tushar Nov 06 '20 at 00:25
  • @bartcubrich Basically I am creating a multi line string which when printed without wrapping text and with monospace fonts, it looks like a table. – Tushar Nov 06 '20 at 00:27
  • Thanks @Tushar. First, you should post some code, not all of it. See [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example). Second, I don't think it is desirable to depend on the table output. How big is the console window? What resolution is the screen. Is it required to work that way for you school project? – bart cubrich Nov 06 '20 at 00:30
  • 2
    Changing the terminal font from within an application sounds impossible, but I don't think anyone uses non-monospace fonts in terminal. About the wrapping problem, I guess the most elegant thing to do would be to truncate the table to the [width of the terminal](https://stackoverflow.com/questions/566746/how-to-get-linux-console-window-width-in-python) which in a small window would result in seeing only the first couple of columns, but at least you could make sense out of them. – Czaporka Nov 06 '20 at 00:37
  • Depending on what operating system / console / terminal you're using, this may not be impossible. Please edit your question and add this missing information (or tag it appropriately). – martineau Nov 06 '20 at 01:08

1 Answers1

1

I don't think you'll see non-monospaced fonts in a console. However, the width and height of the console window could be an issue - in Windows, you can change the size of the console window by running mode width, height for example mode 120,40. You can issue that command from your Python script as well.

import os

os.system('mode 180,20')
print('Hello wide world!')
os.system('pause')

This example demonstrates the effect you appear to be after.

Note that this won't affect specific consoles, for example the console your script will run in from an IDE like PyCharm - it assumes your code is running in a normal Windows console window.

Generally speaking, I'd recommend against solutions like these. They make your script specific to a particular OS and a particular way of running at that. It's often better to come up with solutions that will work mostly anywhere.

Grismar
  • 27,561
  • 4
  • 31
  • 54
  • Wow it works, Thank you. Can you also tell how to add the following : Font = Consolas (Bold) size=20 and color=green ? – Tushar Nov 06 '20 at 05:32
  • That's not something that can be controlled from the console - the font of the windows is an aspect that windows controls from the outside. However, you could look into ANSI colour-coding, but I would recommend against it. This line of development is likely a dead end and a waste of your time to learn to do. – Grismar Nov 06 '20 at 06:01