I just installed termcolor for Python 2.7 on Windows. When I try to print colored text, I get the color codes instead.
from termcolor import colored
print co
Did work :
inserting previous to importing termcolor:
import subprocess
subprocess.call('', shell=True)
Didn't work:
Can't explain why it works, only that I was able to compare one script that colors worked correctly, and one that didn't work correctly.
In termcolor2 module you must type this:
import termcolor2
import colorama
colorama.init()
myText = input("Type a text : ")
color = input("What color you want? : ")
print(termcolor2.colored(myText, color))
That's it...
To make the ANSI colors used in termcolor work with the windows terminal, you'll need to also import/init colorama;
>>> from termcolor import *
>>> cprint('hello', 'red')
←[31mhello←[0m
>>> import colorama
>>> colorama.init()
>>> cprint('hello', 'red')
hello <-- in red color
>>>
Here is a simple function I find useful to print in color. You do not need to make any imports and you do not have to remember complex ANSI codes. The function uses standard RGB tuples to define the foreground and background color.You can find a RGB color picker at https://www.google.com/search?q=rgb+color+picker&oq=rgb+color+picker&aqs=chrome..69i57j0l7.5967j0j8&sourceid=chrome&ie=UTF-8
def print_in_color(txt_msg,fore_tupple,back_tupple,):
#prints the text_msg in the foreground color specified by fore_tupple with the background specified by back_tupple
#text_msg is the text, fore_tupple is foregroud color tupple (r,g,b), back_tupple is background tupple (r,g,b)
rf,gf,bf=fore_tupple
rb,gb,bb=back_tupple
msg='{0}' + txt_msg
mat='\33[38;2;' + str(rf) +';' + str(gf) + ';' + str(bf) + ';48;2;' + str(rb) + ';' +str(gb) + ';' + str(bb) +'m'
print(msg .format(mat))
print('\33[0m') # returns default print color to back to black
# example of use using a message with variables
fore_color='cyan'
back_color='dark green'
msg='foreground color is {0} and the background color is {1}'.format(fore_color, back_color)
print_in_color(msg, (0,255,255),(0,127,127))
windows command prompt uses a command to change the terminal output colour. you can execute the command 'color color-code' to change the color instantly. Just having the command color activates this color feature.
In short.. For your script to work, Run this at the start of your script.
import os
os.system('color')