如何在Python中将彩色文本输出到终端?


当前回答

考虑到您是否正在编写命令行工具,这是最简单和方便的方法。这种方法可以在所有控制台上的任何地方工作,而无需安装任何花哨的软件包。

要使ANSI代码在Windows上运行,首先,运行os.system('color')

import os
os.system('color')

COLOR = '\033[91m'  # change it, according to the color need

END = '\033[0m'

print(COLOR + "Hello World" + END) #print a message


exit=input() #to avoid closing the terminal windows


更多颜色:

注意:\33[5m和\33[6m闪烁。

感谢@qubodup

其他回答

我编写了一个在Linux、OS X和Windows中处理颜色的模块。它支持所有平台上的所有16种颜色,您可以在不同的时间设置前景和背景颜色,字符串对象可以为len()和.capitalize()等对象提供正常的结果。

https://github.com/Robpol86/colorclass

您可以使用任何语言提供的shell转义字符。这些转义字符以ESC字符开头,后跟一些参数。

例如,要在终端中输出红色“Hello,World!”字符串:

echo "\e[31m Hello, World! \e[0m"

或者从Python脚本:

print("\e[31m Hello world \e[0m")

此外,我写了一篇关于Escape序列的文章,这可能会帮助您更好地理解这个机制。

这里有一个简单的函数,我使用它来打印彩色文本消息,而不必记住ANSI代码,而是使用标准RGB元组来定义前景和背景颜色。

def print_in_color(txt_msg, fore_tuple, back_tuple, ):
    # Prints the text_msg in the foreground color specified by fore_tuple with the background specified by back_tuple
    # text_msg is the text, fore_tuple is foreground color tuple (r,g,b), back_tuple is background tuple (r,g,b)
    rf,bf,gf = fore_tuple
    rb,gb,bb = back_tuple
    msg = '{0}' + txt_msg
    mat = '\33[38;2;' + str(rf) + ';' + str(gf) + ';' + str(bf) + ';48;2;' + str(rb) + ';' +str(gb) + ';' + str(bb) + 'm'
    print(msg .format(mat))
    print('\33[0m') # Returns default print color to back to black

# Example of use using a message with variables
fore_color = 'cyan'
back_color = 'dark green'
msg = 'foreground color is {0} and the background color is {1}'.format(fore_color, back_color)
print_in_color(msg, (0,255,255), (0,127,127))

对于Windows,除非使用Win32 API,否则无法使用颜色打印到控制台。

对于Linux,它就像使用打印一样简单,转义序列如下:

颜色

要使字符像方框一样打印,这实际上取决于控制台窗口使用的字体。磅符号效果很好,但它取决于字体:

#

我有一个叫做colorist的图书馆。这非常简单。

以下是一些示例:

from colorit import *

# Use this to ensure that ColorIt will be usable by certain command line interfaces
# Note: This clears the terminal
init_colorit()

# Foreground
print(color("This text is red", Colors.red))
print(color("This text is orange", Colors.orange))
print(color("This text is yellow", Colors.yellow))
print(color("This text is green", Colors.green))
print(color("This text is blue", Colors.blue))
print(color("This text is purple", Colors.purple))
print(color("This text is white", Colors.white))

# Background
print(background("This text has a background that is red", Colors.red))
print(background("This text has a background that is orange", Colors.orange))
print(background("This text has a background that is yellow", Colors.yellow))
print(background("This text has a background that is green", Colors.green))
print(background("This text has a background that is blue", Colors.blue))
print(background("This text has a background that is purple", Colors.purple))
print(background("This text has a background that is white", Colors.white))

# Custom
print(color("This color has a custom grey text color", (150, 150, 150)))
print(background("This color has a custom grey background", (150, 150, 150)))

# Combination
print(
    background(
        color("This text is blue with a white background", Colors.blue), Colors.white
    )
)

# If you are using Windows Command Line, this is so that it doesn't close immediately
input()

这为您提供了:

值得注意的是,这是跨平台的,已经在Mac、Linux和Windows上进行了测试。

你可能想试试:https://github.com/SuperMaZingCoder/colorit

coloret现在可以与PyPi一起安装了!您可以在Windows上使用pipinstall-color-it安装它,在macOS和Linux上使用pip3 install-color-it安装它。