import sys from termcolor import colored, cprint text = colored('Hello, World!', 'red', attrs=['reverse', 'blink']) print(text) cprint('Hello, World!', 'green', 'on_red') print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan') print_red_on_cyan('Hello, World!') print_red_on_cyan('Hello, Universe!') for i in range(10): cprint(i, 'magenta', end=' ') cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)
Text colors:
- grey
- red
- green
- yellow
- blue
- magenta
- cyan
- white
Text highlights:
- on_grey
- on_red
- on_green
- on_yellow
- on_blue
- on_magenta
- on_cyan
- on_white
Attributes:
- bold
- dark
- underline
- blink
- reverse
- concealed
我们用ipython工具显示termcolor 模块的功能:
显示termcolor主要功能
In [3]: from termcolor import
ATTRIBUTES HIGHLIGHTS VERSION cprint print_function
COLORS RESET colored os
文本颜色常量字典
In [5]: from termcolor import COLORS as textcolors
In [6]: print textcolors
{'blue': 34, 'grey': 30, 'yellow': 33, 'green': 32, 'cyan': 36, 'magenta': 35, 'white': 37, 'red': 31}
高亮文本常量字典
print HIGHLIGHTS
{'on_cyan': 46, 'on_white': 47, 'on_grey': 40, 'on_yellow': 43, 'on_blue': 44, 'on_magenta': 45, 'on_red': 41, 'on_green': 42}
知道这些文本常量,我们就可以随机显示打印文本的颜色!是不是很方便。