如何在windows cmd中打印彩色pyfiglet文本

2024-05-02 08:47:53 发布

您现在位置:Python中文网/ 问答频道 /正文

您好,我正在使用Python,我想在使用figlet_格式时用颜色打印程序名,因此我在vscode中工作正常,但当我尝试在cmd中运行文件时,它会打印以下内容:

[34m _   _      _ _
| | | | ___| | | ___
| |_| |/ _ \ | |/ _ \
|  _  |  __/ | | (_) |
|_| |_|\___|_|_|\___/

[0m

代替在vscode中打印内容:

enter image description here

所以我想知道在使用pyfiglet时是否有一种在cmd中打印颜色的方法,这是我的代码:

from termcolor import colored
from pyfiglet import figlet_format

print((colored(figlet_format("Hello"), color="blue")))

Tags: 文件方法fromimport程序cmdformat内容
2条回答

您也可以尝试此新库Printy 刚刚发布了版本1.2.0作为跨平台库

请查看: Printy on github

它是基于旗帜的,所以你可以做像这样的事情

from printy import printy

# with global flags, this will apply a bold (B) red (r) color and an underline (U) to the whole text
printy("Hello world", "rBU")

# with inline formats, this will apply a dim(D)
#blue (b) to the word 'Hello' and a striked (S)
#yellow (y) to the word 'world', the rest will remain as the predefined format
printy("this is a [bD]Hello@ [yS]world@ text")

enter image description here

在Windows上,您需要安装Colorama

Makes ANSI escape character sequences (for producing colored terminal text and cursor positioning) work under MS Windows.

相关问题 更多 >