Search code examples
pythonuser-interfacetextcommand-linelogging

How to easily print ascii-art text?


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?


Solution

    • 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 
    

    missile

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