python输出颜色(终端控制台)
- 1、终端ANSI
- 2、Python自带的函数库ctypes
- 3、colorama
1、终端ANSI
更多查看 ECHOX.bat输出文本背景和文字颜色
其中特殊字符
print("Black :[30m f0 [0m Black :[40m b0 [0m")
即是print("Black :\033[30m f0 \033[0m Black :\033[40m b0 \033[0m")
(特殊字符\033
)
PyCharm终端正常,cmd终端异常
# coding=utf-8
if __name__ == '__main__':
print("ANSI-Color")
print(" ---Foreground---")
print("30 Black :\033[30mhello world!\033[0m")
print("31 Red :\033[31mhello world!\033[0m")
print("32 Green :\033[32mhello world!\033[0m")
print("33 Yellow :\033[33mhello world!\033[0m")
print("34 Blue :\033[34mhello world!\033[0m")
print("35 Magenta :\033[35mhello world!\033[0m")
print("36 Cyan :\033[36mhello world!\033[0m")
print("37 White :\033[37mhello world!\033[0m")
print(" ---Background---")
print("40 Black :\033[40mhello world!\033[0m")
print("41 Red :\033[41mhello world!\033[0m")
print("42 Green :\033[42mhello world!\033[0m")
print("43 Yellow :\033[43mhello world!\033[0m")
print("44 Blue :\033[44mhello world!\033[0m")
print("45 Magenta :\033[45mhello world!\033[0m")
print("46 Cyan :\033[46mhello world!\033[0m")
print("47 White :\033[47mhello world!\033[0m")
print(" ---Foreground;Background---")
print("\033[30;40m 00 \033[30;41m 01 \033[30;42m 02 \033[30;43m 03 \033[30;44m 04 \033[30;45m 05 \033[30;46m 06 \033[30;47m 07 \033[0m")
print("\033[31;40m 10 \033[31;41m 11 \033[31;42m 12 \033[31;43m 13 \033[31;44m 14 \033[31;45m 15 \033[31;46m 16 \033[31;47m 17 \033[0m")
print("\033[32;40m 20 \033[32;41m 21 \033[32;42m 22 \033[32;43m 23 \033[32;44m 14 \033[32;45m 15 \033[32;46m 16 \033[32;47m 17 \033[0m")
print("\033[33;40m 30 \033[33;41m 31 \033[33;42m 32 \033[33;43m 33 \033[33;44m 14 \033[33;45m 15 \033[33;46m 16 \033[33;47m 17 \033[0m")
print("\033[34;40m 40 \033[34;41m 41 \033[34;42m 42 \033[34;43m 43 \033[34;44m 14 \033[34;45m 15 \033[34;46m 16 \033[34;47m 17 \033[0m")
print("\033[35;40m 50 \033[35;41m 51 \033[35;42m 52 \033[35;43m 53 \033[35;44m 14 \033[35;45m 15 \033[35;46m 16 \033[35;47m 17 \033[0m")
print("\033[36;40m 60 \033[36;41m 61 \033[36;42m 62 \033[36;43m 63 \033[36;44m 14 \033[36;45m 15 \033[36;46m 16 \033[36;47m 17 \033[0m")
print("\033[37;40m 70 \033[37;41m 71 \033[37;42m 72 \033[37;43m 73 \033[37;44m 14 \033[37;45m 15 \033[37;46m 16 \033[37;47m 17 \033[0m")
print("---Light;Foreground;Background---")
print("\033[1;30;40m 00 \033[1;30;41m 01 \033[1;30;42m 02 \033[1;30;43m 03 \033[1;30;44m 04 \033[1;30;45m 05 \033[1;30;46m 06 \033[1;30;47m 07 \033[0m")
print("\033[1;31;40m 10 \033[1;31;41m 11 \033[1;31;42m 12 \033[1;31;43m 13 \033[1;31;44m 14 \033[1;31;45m 15 \033[1;31;46m 16 \033[1;31;47m 17 \033[0m")
print("\033[1;32;40m 20 \033[1;32;41m 21 \033[1;32;42m 22 \033[1;32;43m 23 \033[1;32;44m 14 \033[1;32;45m 15 \033[1;32;46m 16 \033[1;32;47m 17 \033[0m")
print("\033[1;33;40m 30 \033[1;33;41m 31 \033[1;33;42m 32 \033[1;33;43m 33 \033[1;33;44m 14 \033[1;33;45m 15 \033[1;33;46m 16 \033[1;33;47m 17 \033[0m")
print("\033[1;34;40m 40 \033[1;34;41m 41 \033[1;34;42m 42 \033[1;34;43m 43 \033[1;34;44m 14 \033[1;34;45m 15 \033[1;34;46m 16 \033[1;34;47m 17 \033[0m")
print("\033[1;35;40m 50 \033[1;35;41m 51 \033[1;35;42m 52 \033[1;35;43m 53 \033[1;35;44m 14 \033[1;35;45m 15 \033[1;35;46m 16 \033[1;35;47m 17 \033[0m")
print("\033[1;36;40m 60 \033[1;36;41m 61 \033[1;36;42m 62 \033[1;36;43m 63 \033[1;36;44m 14 \033[1;36;45m 15 \033[1;36;46m 16 \033[1;36;47m 17 \033[0m")
print("\033[1;37;40m 70 \033[1;37;41m 71 \033[1;37;42m 72 \033[1;37;43m 73 \033[1;37;44m 14 \033[1;37;45m 15 \033[1;37;46m 16 \033[1;37;47m 17 \033[0m")
2、Python自带的函数库ctypes
ctypes函数库Python 2.5引入,Python 标准库用来调用 C 代码的外来函数接口。
ctypes中如下代码:主要是color是由2位十六进制组成,分别取0~f,前一位指的是背景色,后一位指的是字体色
(调用到dll库SetConsoleTextAttribute、GetStdHandle)
# ! /usr/bin/env python
# coding=utf-8
import ctypes
# get handle
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
def set_cmd_text_color(color, handle=std_out_handle):
Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return Bool
# reset white
def resetColor():
set_cmd_text_color(FOREGROUND_WHITE)
def printX(text, color):
set_cmd_text_color(color)
# sys.stdout.write(text)
print(text)
resetColor()
if __name__ == '__main__':
printX("FOREGROUND_BLACK", 0x00)
printX("FOREGROUND_DARKBLUE", 0x01)
printX("FOREGROUND_DARKGREEN", 0x02)
printX("FOREGROUND_DARKSKYBLUE", 0x03)
printX("FOREGROUND_DARKRED", 0x04)
printX("FOREGROUND_DARKPINK", 0x05)
printX("FOREGROUND_DARKYELLOW", 0x06)
printX("FOREGROUND_DARKWHITE", 0x07)
printX("FOREGROUND_DARKGRAY", 0x08)
printX("FOREGROUND_BLUE", 0x09)
printX("FOREGROUND_GREEN", 0x0a)
printX("FOREGROUND_SKYBLUE", 0x0b)
printX("FOREGROUND_RED", 0x0c)
printX("FOREGROUND_PINK", 0x0d)
printX("FOREGROUND_YELLOW", 0x0e)
printX("FOREGROUND_WHITE", 0x0f)
printX("BACKGROUND_BLUE", 0x10)
printX("BACKGROUND_GREEN", 0x20)
printX("BACKGROUND_DARKSKYBLUE", 0x30)
printX("BACKGROUND_DARKRED", 0x40)
printX("BACKGROUND_DARKPINK", 0x50)
printX("BACKGROUND_DARKYELLOW", 0x60)
printX("BACKGROUND_DARKWHITE", 0x70)
printX("BACKGROUND_DARKGRAY", 0x80)
printX("BACKGROUND_BLUE", 0x90)
printX("BACKGROUND_GREEN", 0xa0)
printX("BACKGROUND_SKYBLUE", 0xb0)
printX("BACKGROUND_RED", 0xc0)
printX("BACKGROUND_PINK", 0xd0)
printX("BACKGROUND_YELLOW", 0xe0)
printX("BACKGROUND_WHITE", 0xf0)
print("xhbruce")
printX("BACKGROUND_YELLOW | FOREGROUND_RED", 0xe0 | 0x0c)
3、colorama
colorama HomePage
colorama PyPI
Fore 字体颜色 | Back 字体背景颜色 | Style 字体格式 |
---|---|---|
BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET LIGHTBLACK_EX, LIGHTRED_EX, LIGHTGREEN_EX, LIGHTYELLOW_EX, LIGHTBLUE_EX, LIGHTMAGENTA_EX, LIGHTCYAN_EX, LIGHTWHITE_EX | BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET LIGHTBLACK_EX, LIGHTRED_EX, LIGHTGREEN_EX, LIGHTYELLOW_EX, LIGHTBLUE_EX, LIGHTMAGENTA_EX, LIGHTCYAN_EX, LIGHTWHITE_EX | DIM, NORMAL, BRIGHT, RESET_ALL |
Style.RESET_ALL重置前景、背景和亮度
# ! /usr/bin/env python
# coding=utf-8
from colorama import init, Fore, Back, Style
init(autoreset=True)
if __name__ == '__main__':
print(Fore.RED + 'I am red!' + Fore.RESET)
print(Fore.GREEN + 'I am gree!' + Fore.RESET)
print(Fore.YELLOW + 'I am yellow!' + Fore.RESET)
print(Fore.BLUE + 'I am blue!' + Fore.RESET)
print(Fore.MAGENTA + 'I am magenta!' + Fore.RESET)
print(Fore.CYAN + 'I am cyan!' + Fore.RESET)
print(Fore.WHITE + 'I am white!' + Fore.RESET)
print(Fore.WHITE + Back.GREEN + 'I am white green!' + Fore.RESET + Back.RESET)
print(Style.BRIGHT + Fore.GREEN + "www.cndba.cn")
print('xhbruce')
print(Fore.RED + 'some red text')
print('automatically back to default color again')