如何轻松打印 ascii-art 文本? [英] How to easily print ascii-art text?

查看:16
本文介绍了如何轻松打印 ascii-art 文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个转储大量输出的程序,并且我希望其中一些输出非常突出.一种方法是使用 ascii 艺术渲染重要文本,例如此网络服务 例如:

 ############################################################################################################

其他解决方案可以是彩色或粗体输出.那么如何在 Python 中轻松完成这类事情呢?

解决方案

  • pyfiglet - http://www.figlet.org

    的纯 Python 实现

    pip install pyfiglet

  • termcolor - ANSI 颜色格式的辅助函数

    pip install termcolor

  • colorama - 多平台支持 (Windows)

    pip install colorama

导入系统从 colorama 导入初始化init(strip=not sys.stdout.isatty()) # 如果标准输出被重定向,则去除颜色从 termcolor 导入 cprint从 pyfiglet 导入 figlet_formatcprint(figlet_format('导弹!', font='starwars'),'黄色', 'on_red', attrs=['bold'])

示例

$ python print-warning.py

<前>$ python 打印警告.py |猫.___ ___.________._______.__ __ ___ __|/|||/|/||||||____||||/||||(----` | (----`| | | | | |__ | |||/|||| |||||__|||||||||.----) |.----) ||||`----.||____ |__||__||__||__||_______/|_______/|__||_______||_______|(__)

I have a program that dumps a lot of output, and I want some of that output to really stand out. One way could be to render important text with ascii art, like this web service does for example:

 #    #   ##   #####  #    # # #    #  ####  
 #    #  #  #  #    # ##   # # ##   # #    # 
 #    # #    # #    # # #  # # # #  # #      
 # ## # ###### #####  #  # # # #  # # #  ### 
 ##  ## #    # #   #  #   ## # #   ## #    # 
 #    # #    # #    # #    # # #    #  ####  

other solutions could be colored or bold output. So how to do this sort of stuff easily in Python?

解决方案

  • pyfiglet - pure Python implementation of http://www.figlet.org

    pip install pyfiglet
    

  • termcolor - helper functions for ANSI color formatting

    pip install termcolor
    

  • colorama - multiplatform support (Windows)

    pip install colorama
    

import sys

from colorama import init
init(strip=not sys.stdout.isatty()) # strip colors if stdout is redirected
from termcolor import cprint 
from pyfiglet import figlet_format

cprint(figlet_format('missile!', font='starwars'),
       'yellow', 'on_red', attrs=['bold'])

Example

$ python print-warning.py 

$ python print-warning.py | cat
.___  ___.  __       _______.     _______. __   __       _______  __
|   /   | |  |     /       |    /       ||  | |  |     |   ____||  |
|    /  | |  |    |   (----`   |   (----`|  | |  |     |  |__   |  |
|  |/|  | |  |                       |  | |  |     |   __|  |  |
|  |  |  | |  | .----)   |   .----)   |   |  | |  `----.|  |____ |__|
|__|  |__| |__| |_______/    |_______/    |__| |_______||_______|(__)

这篇关于如何轻松打印 ascii-art 文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆