#! /usr/bin/env python# -*- coding: utf-8 -*-################################### ############################################### copyright (c) 2014 baidu.com, inc. all rights reserved########################################################################### ###### "" "Windows command line Color output @authors : guoxiaohui01<[email Protected]> @copyright : baidu@date : 2014-10-09@version : 1.0.0.0 "" "import Ctypesclass cwincolor (object): " windows Platform color output " @staticmethod def color_print (Print_text, color_type): "" " ColorPrint (red) + (green) = (yellow) (blue) + (green) = (cyan) (red) + (blue) = (magenta) input: color_type: red|green|blue|yellow|cyan|pink "" " std_input_ handle = -10 std_output_handle= -11 STD_ERROR_HANDLE = -12 #前景字体 FOREGROUND_BLUE = 0x01 FOREGROUND_GREEN = 0x02 foreground_red = 0x04 foreground_intensity = 0x08 #背景 background_blue = 0x10 background_green= 0x20 BACKGROUND_RED = 0x40 background_intensity = 0x80 color_red = foreground_red color_green = foreground_green color_blue = FOREGROUND_BLUE color_yellow = FOREGROUND_GREEN | FOREGROUND_RED color_cyan = FOREGROUND_BLUE | FOREGROUND_GREEN color_pink = FOREGROUND_RED | FOREGROUND_BLUE intensity = FOREGROUND_INTENSITY sTd_out_handle = ctypes.windll.kernel32.getstdhandle (Std_output_handle) if (color_type == "Red"): Ctypes.windll.kernel32.SetConsoleTextAttribute (std_out_handle, color_red | intensity) if (color_type == "green"): ctypes.windll.kernel32.setconsoletextattribute (Std_out_handle, color_green | intensity) if (color_type == "Blue"): Ctypes.windll.kernel32.SetConsoleTextAttribute (std_out_handle, color_blue | intensity) if (color_type == "yellow"): ctypes.windll.kernel32.setconsoletextattribute (std_out_handle, color_yellow | intensity) if (color_type == "Cyan"): Ctypes.windll.kernel32.SetConsoleTextAttribute (std_out_handle, color_cyan | intensity) if (color_type == "Pink"): ctypes.windll.kernel32.setconsoletextattribute (Std_out_handle, color_pink | intensity) print print_text ctypes.windll.kernel32.setconsoletextattribute (Std_out_handle, color_red | color_blue | color_green) DEF&NBSp;demo (): "" " Test Case " " cwincolor.color_print ("This is Red". Decode ("UTF8"). Encode ("GBK"), "Red") Cwincolor.color_print ("This is green". Decode ("UTF8"). Encode ("GBK"), "green") Cwincolor.color_print ("This is Blue". Decode ("UTF8"). Encode ("GBK"), "Blue") cwincolor.color _print ("This is yellow". Decode ("UTF8"). Encode ("GBK"), "Yellow") cwincolor.color_print ("This is pink" . Decode ("UTF8"). Encode ("GBK"), "pink") if __name__ == "__main__": Demo ()
#! /usr/bin/env python# -*- coding: utf-8 -*-import sysif Sys.platform.startswith ("Win"): import win_color print_red = lambda (Input): Win_color. Cwincolor.color_print (input, "Red") print_green = lambda (Input): Win_color. Cwincolor.color_print (input, "green") print_blue = lambda (Input): Win_color. Cwincolor.color_print (input, "Blue") print_yellow = lambda ( Input): Win_color. Cwincolor.color_print (input, "Yellow") print_cyan = lambda (Input): Win_color. Cwincolor.color_print (input, "cyan") print_pink = lambda ( Input): Win_color. Cwincolor.color_print (input, "Pink") else: import termcolor print_red = lambda (Input): Termcolor.cprint (input, ' red ', attrs=[' bold ') print_blue = lambda (Input): Termcolor.cprint (input, ' Blue ', attrs=[' bold ') print_green = lambda (Input): Termcolor.cprint (input, ' green ', attrs=[' Bold ']) print_yellow = lambda (input): Termcolor.cprint (input, ' Yellow ', attrs=[' bold ') print_cyan = lambda (input): Termcolor.cprint (input, ' cyan ', attrs=[' bold ')) print_pink = lambda (Input): Termcolor.cprint (input, ' Magenta ', attrs=[' bold ') Print_pink ("Pink") Print_green ( "Green") Print_yellow ("Yellow") print_red ("Red") Print_cyan ("Cyan") Print_blue ("Blue")
The effect is as follows:
Linux&dos Terminal Color Printing