Based on Win7 + python3.4
ImportcTYPESImportSYS" "Windows cmd command line color" "#Handle numberStd_input_handle =-10Std_output_handle=-11Std_error_handle=-12#Front ViewForeground_black = 0x0#BlackForeground_blue = 0x01#BlueForeground_green = 0x02#Greenforeground_red = 0x04#RedForeground_intensity = 0x08#Highlight#Background ColorBackground_blue = 0x10#BlueBackground_green = 0x20#Greenbackground_red = 0x40#RedBackground_intensity = 0x80#HighlightColors= [Foreground_blue,#Blue WordForeground_green,#Green WordForeground_red,#Scarlet LetterForeground_blue | Foreground_intensity,#Blue Word (highlight)Foreground_green | Foreground_intensity,#Green Word (highlight)foreground_red | Foreground_intensity,#Scarlet Letter (highlighted)foreground_red | foreground_intensity| Background_blue | Background_intensity]#Red Letter Blue BottomTexts= ['Blue Word', 'Green Word', 'Scarlet Letter', 'Blue Word (highlight)', 'Green Word (highlight)', 'Scarlet Letter (highlighted)', 'Red Letter Blue Bottom'] #See "Http://msdn.microsoft.com/library/default.asp?url=/library/en-us/winprog/winprog/windows_api_ Reference.asp "For information on Windows APIs.Std_out_handle=Ctypes.windll.kernel32.GetStdHandle (Std_output_handle)defSet_cmd_color (color, handle=std_out_handle): bool=Ctypes.windll.kernel32.SetConsoleTextAttribute (handle, color)returnBOOLdefReset_color (): Set_cmd_color (foreground_red| Foreground_green |foreground_blue)defprint_color_text (color, text): Set_cmd_color (color) sys.stdout.write ('%s\n'% text)#==> Print (text)Reset_color ()defprint_colors_texts (colors, texts): forColor, textinchZip (colors, texts): Print_color_text (color, text)if __name__=="__main__": print_colors_texts (colors, texts)
Python control cmd command line color