Python终端打印彩色文本的方法

Python终端打印彩色文本的方法

技术背景

在Python开发中,有时需要在终端打印彩色文本,以突出显示重要信息、区分不同类型的输出等。实现这一功能的方法因平台而异,常见的是使用ANSI转义序列,也可以借助第三方库来简化操作。

实现步骤

1. 使用ANSI转义序列

ANSI转义序列是一种用于控制终端文本格式和颜色的特殊字符序列。以下是一个简单的Python类示例:

1
2
3
4
5
6
7
8
9
10
11
12
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKCYAN = '\033[96m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)

这种方法在Unix系统(包括OS X、Linux)和支持ANSI转义序列的Windows系统(如使用ANSICON或在Windows 10中启用VT100仿真)上都可以工作。

2. 使用Python termcolor模块

termcolor模块提供了更简单的方式来打印彩色文本:

1
2
3
from termcolor import colored

print(colored('hello', 'red'), colored('world', 'green'))

3. 使用Colorama库

Colorama是一个跨平台的彩色文本输出库,支持Python 3.5+和Python 2.7:

1
2
3
4
5
6
7
from colorama import init as colorama_init
from colorama import Fore
from colorama import Style

colorama_init()

print(f"This is {Fore.GREEN}color{Style.RESET_ALL}!")

4. 使用sty库

sty库支持8位和24位(RGB)颜色,允许自定义样式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
from sty import fg, bg, ef, rs

foo = fg.red + 'This is red text!' + fg.rs
bar = bg.blue + 'This has a blue background!' + bg.rs
baz = ef.italic + 'This is italic text' + rs.italic
qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs
qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs

# Add custom colors:
from sty import Style, RgbFg
fg.orange = Style(RgbFg(255, 150, 50))
buf = fg.orange + 'Yay, Im orange.' + fg.rs

print(foo, bar, baz, qux, qui, buf, sep='\n')

5. 使用Rich库

Rich是一个用于在终端处理颜色和格式化的Python库,支持BBCode-like语法:

1
2
from rich import print
print("[red]Color[/] in the [bold magenta]Terminal[/]!")

6. 自定义函数

可以根据RGB值自定义函数来打印彩色文本:

1
2
3
4
5
6
def colored(r, g, b, text):
return f"\033[38;2;{r};{g};{b}m{text}\033[0m"

text = 'Hello, World!'
colored_text = colored(255, 0, 0, text)
print(colored_text)

核心代码

以下是一个完整的示例,展示了多种打印彩色文本的方法:

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
# 使用ANSI转义序列
class bcolors:
HEADER = '\033[95m'
OKBLUE = '\033[94m'
OKGREEN = '\033[92m'
WARNING = '\033[93m'
FAIL = '\033[91m'
ENDC = '\033[0m'
BOLD = '\033[1m'
UNDERLINE = '\033[4m'

print(bcolors.WARNING + "Warning: No active frommets remain. Continue?" + bcolors.ENDC)

# 使用termcolor模块
from termcolor import colored
print(colored('hello', 'red'), colored('world', 'green'))

# 使用Colorama库
from colorama import init as colorama_init
from colorama import Fore
from colorama import Style
colorama_init()
print(f"This is {Fore.GREEN}color{Style.RESET_ALL}!")

# 使用sty库
from sty import fg, bg, ef, rs
foo = fg.red + 'This is red text!' + fg.rs
bar = bg.blue + 'This has a blue background!' + bg.rs
baz = ef.italic + 'This is italic text' + rs.italic
qux = fg(201) + 'This is pink text using 8bit colors' + fg.rs
qui = fg(255, 10, 10) + 'This is red text using 24bit colors.' + fg.rs
from sty import Style, RgbFg
fg.orange = Style(RgbFg(255, 150, 50))
buf = fg.orange + 'Yay, Im orange.' + fg.rs
print(foo, bar, baz, qux, qui, buf, sep='\n')

# 使用Rich库
from rich import print
print("[red]Color[/] in the [bold magenta]Terminal[/]!")

# 自定义函数
def colored(r, g, b, text):
return f"\033[38;2;{r};{g};{b}m{text}\033[0m"

text = 'Hello, World!'
colored_text = colored(255, 0, 0, text)
print(colored_text)

最佳实践

  • 跨平台兼容性:如果需要在不同平台上运行代码,建议使用Coloramasty等跨平台库。
  • 代码简洁性:对于简单的彩色文本输出,使用自定义函数或termcolor模块可以使代码更简洁。
  • 功能扩展性:如果需要更多的格式化功能,如支持BBCode-like语法,建议使用Rich库。

常见问题

  • Windows系统不显示颜色:在Windows系统上,需要使用Colorama库或启用VT100仿真,或者使用os.system('color')来使ANSI转义序列生效。
  • 样式冲突:手动插入ANSI转义序列时,可能会出现样式冲突的问题。建议使用高级库,如yachalk,它可以自动处理样式重置。

Python终端打印彩色文本的方法
https://119291.xyz/posts/2025-05-09.python-terminal-colored-text-printing-methods/
作者
ww
发布于
2025年5月9日
许可协议