在 Python 中有几种方法可以将彩色文本输出到终端。 最常见的做法是:
(1) 使用内置模块
colorama模块:然后可以使用 Colorama 的 ANSI 转义序列的常量简写来完成彩色文本的跨平台打印:
例一:
# Python program to print
# red text with green background
from colorama import Fore, Back, Style
print(Fore.RED + 'some red text')
print(Back.GREEN + 'and with a green background')
print(Style.DIM + 'and in dim text')
print(Style.RESET_ALL)
print('back to normal now')
结果如下图所示:
例二:
# Python program to print
# green text with red background
from colorama import init
from termcolor import colored
init()
print(colored('Hello, World!', 'green', 'on_red'))
结果如下图所示:
termcolor模块:termcolor 是一个 Python 模块,用于在终端中输出 ANSII 颜色格式。
# Python program to print
# colored text and background
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)
结果如下所示:
(2) 使用 ANSI 转义码
打印彩色文本最常用的方法是直接打印 ANSI 转义序列。 这可以以不同的格式交付,例如:
构建要调用的函数:我们可以构建函数来调用特定颜色命名的函数来执行相关的 ANSI 转义序列。
# Python program to print
# colored text and background
def prRed(skk): print(" 33[91m {} 33[00m" .format(skk))
def prGreen(skk): print(" 33[92m {} 33[00m" .format(skk))
def prYellow(skk): print(" 33[93m {} 33[00m" .format(skk))
def prLightPurple(skk): print(" 33[94m {} 33[00m" .format(skk))
def prPurple(skk): print(" 33[95m {} 33[00m" .format(skk))
def prCyan(skk): print(" 33[96m {} 33[00m" .format(skk))
def prLightGray(skk): print(" 33[97m {} 33[00m" .format(skk))
def prBlack(skk): print(" 33[98m {} 33[00m" .format(skk))
prCyan("Hello World, ")
prYellow("It's")
prGreen("Geeks")
prRed("For")
prGreen("Geeks")
建立一个颜色类:创建一个类来分配背景色和前景色并调用它们。
# Python program to print
# colored text and background
class colors:
'''Colors class:reset all colors with colors.reset; two
sub classes fg for foreground
and bg for background; use as colors.subclass.colorname.
i.e. colors.fg.red or colors.bg.greenalso, the generic bold, disable,
underline, reverse, strike through,
and invisible work with the main class i.e. colors.bold'''
reset=' 33[0m'
bold=' 33[01m'
disable=' 33[02m'
underline=' 33[04m'
reverse=' 33[07m'
strikethrough=' 33[09m'
invisible=' 33[08m'
class fg:
black=' 33[30m'
red=' 33[31m'
green=' 33[32m'
orange=' 33[33m'
blue=' 33[34m'
purple=' 33[35m'
cyan=' 33[36m'
lightgrey=' 33[37m'
darkgrey=' 33[90m'
lightred=' 33[91m'
lightgreen=' 33[92m'
yellow=' 33[93m'
lightblue=' 33[94m'
pink=' 33[95m'
lightcyan=' 33[96m'
class bg:
black=' 33[40m'
red=' 33[41m'
green=' 33[42m'
orange=' 33[43m'
blue=' 33[44m'
purple=' 33[45m'
cyan=' 33[46m'
lightgrey=' 33[47m'
print(colors.bg.green, "SKk", colors.fg.red, "Amartya")
print(colors.bg.lightgrey, "SKk", colors.fg.red, "Amartya")
迭代函数:我们可以设计迭代和自生成 ANSI Escape 序列、函数。
# Python program to print
# colored text and background
def print_format_table():
"""
prints table of formatted text format options
"""
for style in range(8):
for fg in range(30, 38):
s1 = ''
for bg in range(40, 48):
format = ';'.join([str(style), str(fg), str(bg)])
s1 += 'x1b[%sm %s x1b[0m' % (format, format)
print(s1)
print('n')
print_format_table()
参考资源
[1]Print Colors in Python terminal



