Member Avatar

Is it possible to change text colour in the console window with Python?
If so, could anyone tell me how?

Recommended Answers

If you use the Windows OS, you can use this approach:

# display text on a Windows console
# Windows XP with Python27 or Python32

from ctypes import windll

# needed for Python2/Python3 diff
try:
    input = raw_input
except:
    pass

STD_OUTPUT_HANDLE = -11

stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

# …
Jump to Post

All 3 Replies

Check Colorama

pip install colorama
Member Avatar

Hmmm... I have downloaded the module, and moved it the Python27 , but when i try import colorama it says that the module I am looking for doesent exist! What am I doing wrong?

If you use the Windows OS, you can use this approach:

# display text on a Windows console
# Windows XP with Python27 or Python32

from ctypes import windll

# needed for Python2/Python3 diff
try:
    input = raw_input
except:
    pass

STD_OUTPUT_HANDLE = -11

stdout_handle = windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

# look at the output and select the color you want
# for instance hex E is yellow on black
# hex 1E is yellow on blue
# hex 2E is yellow on green and so on
for color in range(0, 75):
    windll.kernel32.SetConsoleTextAttribute(stdout_handle, color)
    print("%X --> %s" % (color, "Have a fine day!"))

input("Press Enter to go on ... ")
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.21 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.