如何在Python中将彩色文本输出到终端?
当前回答
如果您正在使用Django:
>>> from django.utils.termcolors import colorize
>>> print colorize("Hello, World!", fg="blue", bg='red',
... opts=('bold', 'blink', 'underscore',))
Hello World!
>>> help(colorize)
快照:
(我通常在runserver终端上使用彩色输出进行调试,所以我添加了它。)
您可以测试它是否安装在您的机器中:$python-c“import django;print django.VERSION”。要安装它,请查看:如何安装django
试试看!!
其他回答
如果您正在使用Django:
>>> from django.utils.termcolors import colorize
>>> print colorize("Hello, World!", fg="blue", bg='red',
... opts=('bold', 'blink', 'underscore',))
Hello World!
>>> help(colorize)
快照:
(我通常在runserver终端上使用彩色输出进行调试,所以我添加了它。)
您可以测试它是否安装在您的机器中:$python-c“import django;print django.VERSION”。要安装它,请查看:如何安装django
试试看!!
class ColorText:
"""
Use ANSI escape sequences to print colors +/- bold/underline to bash terminal.
Examples
--------
>>> ColorText('HelloWorld').bold()
>>> ColorText('HelloWorld').blue()
>>> ColorText('HelloWorld').bold().custom("#bebebe")
>>> ColorText('HelloWorld').underline().custom('dodgerblue')
>>> ColorText.demo()
Notes
-----
- execute ColorText.demo() for a printout of colors.
"""
@classmethod
def demo(cls):
"""Prints examples of all colors in normal, bold, underline, bold+underline."""
for color in dir(ColorText):
if all([color.startswith("_") is False,
color not in ["bold", "underline", "demo", "custom"],
callable(getattr(ColorText, color))]):
print(getattr(ColorText(color), color)(),
"\t",
getattr(ColorText(f"bold {color}").bold(), color)(),
"\t",
getattr(ColorText(f"underline {color}").underline(), color)(),
"\t",
getattr(ColorText(f"bold underline {color}").underline().bold(), color)())
print(ColorText("Input can also be color hex or R,G,B with ColorText.custom()").bold())
pass
def __init__(self, text: str = ""):
self.text = text
self.ending = "\033[0m"
self.colors = []
pass
def __repr__(self):
return self.text
def __str__(self):
return self.text
def bold(self):
self.text = "\033[1m" + self.text + self.ending
return self
def underline(self):
self.text = "\033[4m" + self.text + self.ending
return self
def green(self):
self.text = "\033[92m" + self.text + self.ending
self.colors.append("green")
return self
def purple(self):
self.text = "\033[95m" + self.text + self.ending
self.colors.append("purple")
return self
def blue(self):
self.text = "\033[94m" + self.text + self.ending
self.colors.append("blue")
return self
def ltblue(self):
self.text = "\033[34m" + self.text + self.ending
self.colors.append("lightblue")
return self
def pink(self):
self.text = "\033[35m" + self.text + self.ending
self.colors.append("pink")
return self
def gray(self):
self.text = "\033[30m" + self.text + self.ending
self.colors.append("gray")
return self
def ltgray(self):
self.text = "\033[37m" + self.text + self.ending
self.colors.append("ltgray")
return self
def warn(self):
self.text = "\033[93m" + self.text + self.ending
self.colors.append("yellow")
return self
def fail(self):
self.text = "\033[91m" + self.text + self.ending
self.colors.append("red")
return self
def ltred(self):
self.text = "\033[31m" + self.text + self.ending
self.colors.append("lightred")
return self
def cyan(self):
self.text = "\033[36m" + self.text + self.ending
self.colors.append("cyan")
return self
def custom(self, *color_hex):
"""Print in custom color, `color_hex` - either actual hex, or tuple(r,g,b)"""
if color_hex != (None, ): # allows printing white on black background, black otherwise
if len(color_hex) == 1:
c = rgb2hex(colorConverter.to_rgb(color_hex[0]))
rgb = ImageColor.getcolor(c, "RGB")
else:
assert (
len(color_hex) == 3
), "If not a color hex, ColorText.custom should have R,G,B as input"
rgb = color_hex
self.text = "\033[{};2;{};{};{}m".format(38, *rgb) + self.text + self.ending
self.colors.append(rgb)
return self
pass
使用pyfancy。这是一种在终端中进行颜色处理的简单方法!
例子:
print(pyfancy.RED + "Hello Red" + pyfancy.END)
这个答案试图通过使用正则表达式为文本块中的关键字着色来扩展将着色文本写入终端的概念。
这个答案还使用了Python库Rich,在前面的问题答案中简要介绍了它。在这个答案中,我使用函数rich.color.ANSI_color_NAMES获取一个随机的颜色列表,用于突出显示预定义的搜索项。
import random
import re as regex
from rich import color
from rich import print
def create_dynamic_regex(search_words):
"""
This function is used to create a dynamic regular expression
string and a list of random colors. Both these elements will
be used in the function colorize_text()
:param search_words: list of search terms
:return: regular expression search string and a list of colors
:rtype: string, list
"""
colors_required = create_list_of_colors(len(search_words))
number_of_search_words = len(search_words)
combined_string = ''
for search_word in search_words:
number_of_search_words -= 1
if number_of_search_words != 0:
current_string = ''.join(r'(\b' + search_word + r'\b)|')
combined_string = (combined_string + current_string)
elif number_of_search_words == 0:
current_string = ''.join(r'(\b' + search_word + r'\b)')
combined_string = (combined_string + current_string)
return combined_string, colors_required
def random_color():
"""
This function is used to create a random color using the
Python package rich.
:return: color name
:rtype: string
"""
selected_color = random.choice(list(color.ANSI_COLOR_NAMES.keys()))
return selected_color
def create_list_of_colors(number_of_colors):
"""
This function is used to generate a list of colors,
which will be used in the function colorize_text()
:param number_of_colors:
:return: list of colors
:rtype: list
"""
list_of_colors = [random_color() for _ in range(number_of_colors)]
return list_of_colors
def colorize_text(text, regex_string, array_of_colors):
"""
This function is used to colorize specific words in a text string.
:param text: text string potentially containing specific words to colorize.
:param regex_string: regular expression search string
:param array_of_colors: list of colors
:return: colorized text
:rtype: string
"""
available_colors = array_of_colors
word_regex = regex.compile(f"{regex_string}", regex.IGNORECASE)
i = 0
output = ""
for word in word_regex.finditer(text):
get_color = available_colors[word.lastindex - 1]
output += "".join([text[i:word.start()],
"[%s]" % available_colors[word.lastindex - 1],
text[word.start():word.end()], "[/%s]" % available_colors[word.lastindex - 1]])
i = word.end()
return ''.join([output, text[word.end():]])
def generate_console_output(text_to_search, words_to_find):
"""
This function is used generate colorized text that will
be outputting to the console.
:param text_to_search: text string potentially containing specific words to colorize.
:param words_to_find: list of search terms.
:return: A string containing colorized words.
:rtype: string
"""
search_terms, colors = create_dynamic_regex(words_to_find)
colorize_html = colorize_text(text_to_search, search_terms, colors)
print(colorize_html)
text = "The dog chased the cat that was looking for the mouse that the dog was playing with."
words = ['dog', 'cat', 'mouse']
generate_console_output(text, words)
以下是上述代码的打印输出:
我创建了两个用于为文本着色的GIST。
彩色文本终端输出彩色文本HTML输出
另一个包装Python3打印功能的PyPI模块:
https://pypi.python.org/pypi/colorprint
如果您也可以从__future_import-print中使用,它可以在Python2.x中使用。下面是模块PyPI页面中的Python 2示例:
from __future__ import print_function
from colorprint import *
print('Hello', 'world', color='blue', end='', sep=', ')
print('!', color='red', format=['bold', 'blink'])
它输出“你好,世界!”,单词为蓝色,感叹号为粗体红色并闪烁。