版权声明:此文首发于我的个人站python在cmd中打印彩色文字,转载请注明出处。

百度来的 颜色值不太准确,下面的是亲测修改的;
单独写了一个文件 printColor.py,使用的时候只要作为模块import进来就行。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
import ctypes

# 【强调】 有蓝色背景色
# 7 = >默认值
# 0 = >黑色
# 1 =蓝
# 2 = >绿色
# 3 = >水
# 4 = >红色
# 5 = >紫色-purple
# 6 = >黄-yellow
# 7 = >白色-white
# 8 = >灰色-grey
# 9 = >淡蓝色-Light blue 【与 蓝色 无异】
# 10 = >【强调】灰-Light grey
# 11 = >【强调】蓝-Light blue
# 12 = >【强调】绿-Light green
# 13 = >【强调】水-Light water
# 14 = >【强调】红-Light red
# 15 = >白-white
STD_INPUT_HANDLE = -10
STD_OUTPUT_HANDLE= -11
STD_ERROR_HANDLE = -12

FOREGROUND_BLACK = 0x0
FOREGROUND_BLUE = 0x01 # text color contains blue.
FOREGROUND_GREEN = 0x02 # text color contains green.
FOREGROUND_WATER = 0x03 # text color contains water.
FOREGROUND_RED = 0x04 # text color contains red.
FOREGROUND_PURPLE = 0x05 # text color contains purple.
FOREGROUND_YELLOW = 0x06 # text color contains yellow.
FOREGROUND_WHITE = 0x07 # text color contains white.
FOREGROUND_GREY = 0x08 # text color contains grey.
FOREGROUND_LIGHTGREY = 0x10 # text color contains light grey.
FOREGROUND_LIGHTBLUE = 0x11 # text color contains light blue.
FOREGROUND_LIGHTGREEN = 0x12 # text color contains light green.
FOREGROUND_LIGHTWATER = 0x13 # text color contains light water.
FOREGROUND_LIGHTRED = 0x14 # text color contains light red.
FOREGROUND_WHITE = 0x15 # text color contains white.

BACKGROUND_BLUE = 0x10 # background color contains blue.
BACKGROUND_GREEN = 0x20 # background color contains green.
BACKGROUND_RED = 0x40 # background color contains red.
BACKGROUND_INTENSITY = 0x80 # background color is intensified.
class Colors:
std_out_handle = ctypes.windll.kernel32.GetStdHandle(STD_OUTPUT_HANDLE)

def set_cmd_color(self, color, handle=std_out_handle):
bool = ctypes.windll.kernel32.SetConsoleTextAttribute(handle, color)
return bool

def reset_color(self):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE)

def print_blue_text(self, print_text):
self.set_cmd_color(FOREGROUND_BLUE | FOREGROUND_GREY)
print (print_text)
self.reset_color()

def print_green_text(self, print_text):
self.set_cmd_color(FOREGROUND_GREEN | FOREGROUND_GREY)
print (print_text)
self.reset_color()

def print_water_text(self, print_text):
self.set_cmd_color(FOREGROUND_WATER | FOREGROUND_GREY)
print (print_text)
self.reset_color()

def print_red_text(self, print_text):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_purple_text(self, print_text):
self.set_cmd_color(FOREGROUND_PURPLE | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_yellow_text(self, print_text):
self.set_cmd_color(FOREGROUND_YELLOW | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_white_text(self, print_text):
self.set_cmd_color(FOREGROUND_WHITE | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_grey_text(self, print_text):
self.set_cmd_color(FOREGROUND_GREY | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_lightgrey_text(self, print_text):
self.set_cmd_color(FOREGROUND_LIGHTGREY | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_lightblue_text(self, print_text):
self.set_cmd_color(FOREGROUND_LIGHTBLUE | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_lightgreen_text(self, print_text):
self.set_cmd_color(FOREGROUND_LIGHTGREEN | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_lightwater_text(self, print_text):
self.set_cmd_color(FOREGROUND_LIGHTWATER | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_lightred_text(self, print_text):
self.set_cmd_color(FOREGROUND_LIGHTRED | FOREGROUND_GREY)
print(print_text)
self.reset_color()

def print_white_text(self, print_text):
self.set_cmd_color(FOREGROUND_WHITE | FOREGROUND_GREY)
print (print_text)
self.reset_color()

def print_red_text_with_blue_bg(self, print_text):
self.set_cmd_color(FOREGROUND_RED | FOREGROUND_GREY | BACKGROUND_BLUE | BACKGROUND_INTENSITY)
print (print_text)
self.reset_color()

例如,在test.py中使用:

1
2
3
4
5
6
7
8
9
10
11
12
import printColor

pc = printColor.Colors() // 实例化

def deco(func):
def _deco(*args, **kw):
pc.print_red_text("before myfunc() called.") // 调用
ret = func(*args, **kw)
pc.print_green_text("after myfunc() called. result: %s" % ret) // 调用
return ret
return _deco
...