在实际运行Python脚本的过程中控制台有时满屏清一色的白色输出很不易于识别,如下图。
因此需要给控制台的输出增加点色彩。总的说来利用Python实现彩色输出还是比较简单的。 经过测试,在Windows系统控制台(CMD)中,需要通过Python调用系统的API来实现。 在Python的控制台中,遵循Python的语法规则即可实现。 在Linux下,直接使用Python语法即可。 而至于在MacOS下,由于没有苹果系统所以没有测试了。 但估计应该也是符合Python语法即可,只有Windows比较特殊一点。 这里简单记录一下用法。
1.代码
由于代码比较简单,下面直接贴代码。
# coding=utf-8
import ctypes
import os
import sys
import platform
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE = -11
STD_ERROR_HANDLE = -12
# 字体颜色定义 ,关键在于颜色编码,由2位十六进制组成,分别取0~f,前一位指的是背景色,后一位指的是字体色
# 由于该函数的限制,应该是只有这16种,可以前景色与背景色组合。也可以几种颜色通过或运算组合,组合后还是在这16种颜色中
# Windows CMD命令行 字体颜色定义 text colors
FOREGROUND_BLACK = 0x00 # black.
FOREGROUND_DARKBLUE = 0x01 # dark blue.
FOREGROUND_DARKGREEN = 0x02 # dark green.
FOREGROUND_DARKSKYBLUE = 0x03 # dark skyblue.
FOREGROUND_DARKRED = 0x04 # dark red.
FOREGROUND_DARKPINK = 0x05 # dark pink.
FOREGROUND_DARKYELLOW = 0x06 # dark yellow.
FOREGROUND_DARKWHITE = 0x07 # dark white.
FOREGROUND_DARKGRAY = 0x08 # dark gray.
FOREGROUND_BLUE = 0x09 # blue.
FOREGROUND_GREEN = 0x0a # green.
FOREGROUND_SKYBLUE = 0x0b # skyblue.
FOREGROUND_RED = 0x0c # red.
FOREGROUND_PINK = 0x0d # pink.
FOREGROUND_YELLOW = 0x0e # yellow.
FOREGROUND_WHITE = 0x0f # white.
# Windows CMD命令行 背景颜色定义 background colors
BACKGROUND_BLUE = 0x10 # dark blue.
BACKGROUND_GREEN = 0x20 # dark green.
BACKGROUND_DARKSKYBLUE = 0x30 # dark skyblue.
BACKGROUND_DARKRED = 0x40 # dark red.
BACKGROUND_DARKPINK = 0x50 # dark pink.
BACKGROUND_DARKYELLOW = 0x60 # dark yellow.
BACKGROUND_DARKWHITE = 0x70 # dark white.
BACKGROUND_DARKGRAY = 0x80 # dark gray.
BACKGROUND_BLUE = 0x90 # blue.
BACKGROUND_GREEN = 0xa0 # green.
BACKGROUND_SKYBLUE = 0xb0 # skyblue.
BACKGROUND_RED = 0xc0 # red.
BACKGROUND_PINK = 0xd0 # pink.
BACKGROUND_YELLOW = 0xe0 # yellow.
BACKGROUND_WHITE = 0xf0 # white.
def set_cmd_text_color(color):
# get handle
handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)
Bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return Bool
# reset white
def resetColor():
set_cmd_text_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)
# dark blue
def printDarkBlue(mess):
set_cmd_text_color(FOREGROUND_DARKBLUE)
sys.stdout.write(mess)
resetColor()
# dark green
def printDarkGreen(mess):
set_cmd_text_color(FOREGROUND_DARKGREEN)
sys.stdout.write(mess)
resetColor()
# dark sky blue
def printDarkSkyBlue(mess):
set_cmd_text_color(FOREGROUND_DARKSKYBLUE)
sys.stdout.write(mess)
resetColor()
# dark red
def printDarkRed(mess):
set_cmd_text_color(FOREGROUND_DARKRED)
sys.stdout.write(mess)
resetColor()
# dark pink
def printDarkPink(mess):
set_cmd_text_color(FOREGROUND_DARKPINK)
sys.stdout.write(mess)
resetColor()
# dark yellow
def printDarkYellow(mess):
set_cmd_text_color(FOREGROUND_DARKYELLOW)
sys.stdout.write(mess)
resetColor()
# dark white
def printDarkWhite(mess):
set_cmd_text_color(FOREGROUND_DARKWHITE)
sys.stdout.write(mess)
resetColor()
# dark gray
def printDarkGray(mess):
set_cmd_text_color(FOREGROUND_DARKGRAY)
sys.stdout.write(mess)
resetColor()
# blue
def printBlue(mess):
set_cmd_text_color(FOREGROUND_BLUE)
sys.stdout.write(mess)
resetColor()
# green
def printGreen(mess):
set_cmd_text_color(FOREGROUND_GREEN)
sys.stdout.write(mess)
resetColor()
# sky blue
def printSkyBlue(mess):
set_cmd_text_color(FOREGROUND_SKYBLUE)
sys.stdout.write(mess)
resetColor()
# red
def printRed(mess):
set_cmd_text_color(FOREGROUND_RED)
sys.stdout.write(mess)
resetColor()
# pink
def printPink(mess):
set_cmd_text_color(FOREGROUND_PINK)
sys.stdout.write(mess)
resetColor()
# yellow
def printYellow(mess):
set_cmd_text_color(FOREGROUND_YELLOW)
sys.stdout.write(mess)
resetColor()
# white
def printWhite(mess):
set_cmd_text_color(FOREGROUND_WHITE)
sys.stdout.write(mess)
resetColor()
# white bkground and black text
def printWhiteBlack(mess):
set_cmd_text_color(FOREGROUND_BLACK | BACKGROUND_WHITE)
sys.stdout.write(mess)
resetColor()
# white bkground and black text
def printWhiteBlack_2(mess):
set_cmd_text_color(0xf0)
sys.stdout.write(mess)
resetColor()
# white bkground and black text
def printYellowRed(mess):
set_cmd_text_color(BACKGROUND_YELLOW | FOREGROUND_RED)
sys.stdout.write(mess)
resetColor()
if __name__ == '__main__':
sysstr = platform.system()
if sysstr == "Windows":
print("This is Windows platform.")
print("For system console, use windows API to control color.")
print("For python console, use python style format to control color.")
print("*" * 50)
print("System console test:")
printDarkBlue('DarkBlue\n')
printDarkGreen('DarkGreen\n')
printDarkSkyBlue('DarkSkyBlue\n')
printDarkRed('DarkRed\n')
printDarkPink('DarkPink\n')
printDarkYellow('DarkYellow\n')
printDarkWhite('DarkWhite\n')
printDarkGray('DarkGray\n')
printBlue('Blue\n')
printGreen('Green\n')
printSkyBlue('SkyBlue\n')
printRed('Red\n')
printPink('Pink\n')
printYellow('Yellow\n')
printWhite('White\n')
printWhiteBlack('printWhiteBlack\n')
printYellowRed('printYellowRed\n')
print("*" * 50)
print("Python console test:")
print('normal')
print('\033[1;31m' + 'red' + '\033[0m')
print('\033[1;32m' + 'green' + '\033[0m')
print('\033[1;34m' + 'blue' + '\033[0m')
print('\033[4;33m' + 'yellow' + '\033[0m')
print('\033[1;30;41m' + 'black' + '\033[0m')
print('\033[1;30;47m' + 'white' + '\033[0m')
print("*" * 50)
elif sysstr == "Linux":
print("This is linux platform.")
print("For linux platform, use python style to contorl color.")
print('normal')
print('\033[1;31m' + 'red' + '\033[0m')
print('\033[1;32m' + 'green' + '\033[0m')
print('\033[1;34m' + 'blue' + '\033[0m')
print('\033[4;33m' + 'yellow' + '\033[0m')
print('\033[1;30;41m' + 'black' + '\033[0m')
print('\033[1;30;47m' + 'white' + '\033[0m')
else:
print('normal')
print('\033[1;31m' + 'red' + '\033[0m')
print('\033[1;32m' + 'green' + '\033[0m')
print('\033[1;34m' + 'blue' + '\033[0m')
print('\033[4;33m' + 'yellow' + '\033[0m')
print('\033[1;30;41m' + 'black' + '\033[0m')
print('\033[1;30;47m' + 'white' + '\033[0m')
以后在Windows上使用时,可以直接拷贝定义的这些变量和函数,然后调用即可。 对于Linux,符合Python规定语法即可。
2.测试
首先是在Windows系统控制台中。 可以看到Python语法是失效的,对系统控制台无效。然后是在Python控制台中。 在这里Windows API失效了,但Python语法有效。最后在Linux上测试,如下。
3.参考资料
- https://www.cnblogs.com/hester/p/5278229.html
本文作者原创,未经许可不得转载,谢谢配合