Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure command prompt explicitly via a python program before its execution?

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 !).

like image 974
Tushar Avatar asked Feb 04 '26 10:02

Tushar


1 Answers

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.

like image 164
Grismar Avatar answered Feb 06 '26 07:02

Grismar



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!