Creating a ‘Talking’ Font with Python? I Discovered a Treasure Library!

Hi, I’m Huajie 🌸, a Python enthusiast who codes late at night and writes technical articles while sipping coffee during the day.

Creating a 'Talking' Font with Python? I Discovered a Treasure Library!

Let me tell you something. Recently, while I was writing a command-line tool, I wanted to add some flashy effects to make it less plain and serious… So, I dove into various ASCII art libraries and ended up being completely captivated by a library called <span>pyfiglet</span>.

This thing is genuinely interesting! With it, you can make the text in your terminal super unique, giving off a vibe of “you can tell at a glance that I’m not an ordinary Python program,” with a touch of flair.

Today, I’ll help you understand it!

What is pyfiglet? Can it be eaten?

To be honest, when I first saw this name, I thought it was some kind of food (it sounds a bit like “fried chicken burger alien” πŸ˜‚). Its full name is: Python FIGlet.

You might be wondering, what is FIGlet then?

Here’s a little background: FIGlet is an old program (from the 1980s) that converts regular text into ASCII art.

For example, if you input “huajie”, it can produce an effect like thisπŸ‘‡

 _                  _ _      
| |__  _   _  __ _ (_|_) ___ 
| '_ \| | | |/ _` || | |/ _ \
| | | | |_| | (_| || | |  __/
|_| |_|\__,_|\__,_|/ |_|
                 |__/        

Doesn’t it have that vibe? It gives a sense of being transported back to the DOS era~ And <span>pyfiglet</span> is its Python version.

How to use pyfiglet? It’s really easy to get started

Other libraries might require installing a bunch of dependencies and configuring environments, but <span>pyfiglet</span> is different; it’s as light as a cloud and as clean as a freshly installed system.

Just one command can do it:

pip install pyfiglet

After installation, you can jump straight into the code:

import pyfiglet

ascii_art = pyfiglet.figlet_format("Hello, huajie!")
print(ascii_art)

Run it and see the effect; doesn’t it instantly make the whole terminal look stylish?✨

 _   _      _ _        _                  _ _      _ 
| | | | ___| | | ___  | |__  _   _  __ _ (_|_) ___| |
| |_| |/ _ \ | |/ _ \ | '_ \| | | |/ _` || | |/ _ \ |
|  _  |  __/ | | (_) || | | | |_| | (_| || | |  __/_|
|_| |_|\___|_|_|\___( )_| |_|\__,_|\__,_|/ |_|
                    |/                 |__/ 

Now compare it with a regular <span>print("Hello, huajie!")</span>, doesn’t it feel like “I’m writing Python Plus”?😏

There are so many font styles, it’s up to you how to play with them

The most charming point about this thing is: there are an outrageous number of font styles!

The first time I used it, I switched through more than ten styles in one go and got hooked for half an hour πŸ˜‚

Try the following code to print all supported font names:

from pyfiglet import Figlet

f = Figlet()
fonts = f.getFonts()
print(fonts)

There are dozens of font styles for you to choose from, such as:

from pyfiglet import Figlet

f = Figlet(font='slant')
print(f.renderText("huajie"))
    __                  _ _    
   / /_  __  ______ _  (_|_)__
  / __ \/ / / / __ \/ / / / _ \
 / / / / /_/ / /_/ / / / /  __/
/_/ /_/\__,_/\__,_/_/ /_/\___/
                 /___/

The output looks particularly like the opening scene of a hacker movie, incredibly cool~

πŸ’‘ A little reminder: Don’t try to memorize the font names; they are case-sensitive, and some even have symbols. It’s recommended to use <span>getFonts()</span> to list them out and copy.

What can it actually be used for?

Don’t think this thing can only be used for flashy effects; I’ve personally tested several practical scenarios:

  1. Welcome screen for command-line tools Opening the program and seeing an artistic line saying “Welcome to Huajie’s Toolbox” πŸš€ instantly elevates the experience, right?

  2. Console mini-games For example, if you write a number guessing game, displaying “YOU WIN!” in ASCII font at the end really enhances the atmosphere πŸŽ‰

  3. Scripts to send notifications to yourself After a scheduled task completes, printing “Task Completed βœ…” with some style makes it more eye-catching.

  4. Code pranks (but don’t go overboard) Once, I helped a friend fix a bug, and I changed his welcome message to a giant line saying “BUG IS BACK”. He almost dropped his phone when he opened the terminal πŸ˜‚

What points are easy to get wrong? Avoid these pitfalls

To be honest, <span>pyfiglet</span> is not hard to use, but there are a few places where you might get stuck:

πŸ’€ No support for Chinese!

This library only supports English characters.

I was foolishly typing “δ½ ε₯½” and nothing came out; I thought I had written the code wrong:

print(pyfiglet.figlet_format("δ½ ε₯½"))

Output:

Solution? There’s no way around it; you can only convert to pinyin or use English phrases as substitutes.

πŸ’₯ Terminal width is insufficient

Some fonts are very wide, producing a large output. If your terminal window is too narrow, it will be truncated, looking like garbled text.

Solution: Just widen the window a bit or use shorter words.

🎨 Want to add some color? pyfiglet doesn’t support it by itself

You might be wondering: Can I make these ASCII fonts colorful?

Of course! But you need to use it in conjunction with other libraries, like <span>termcolor</span> or <span>colorama</span>.

Here’s an example:

from pyfiglet import figlet_format
from termcolor import colored

art = figlet_format("Hello!")
colored_art = colored(art, color="green")
print(colored_art)

The moment the green art appears on the screen, I feel like I’m playing a command-line version of QQ Show πŸ˜‚

Bonus time: How to generate random fonts with it?

Did you think this was it? Hey, there’s more fun to be had~

Using <span>random.choice()</span>, you can output with different fonts each time, making it feel like a lucky draw; every time you hit enter, there’s a surprise 🎁

import pyfiglet
import random
from pyfiglet import Figlet

f = Figlet()
fonts = f.getFonts()
f = pyfiglet.Figlet(font=random.choice(fonts))
print(f.renderText("huajie"))

Run it a few times, and you’ll find you can’t stop…

In conclusion, let me ramble a bit:

This library is neither too big nor too small. Although it’s just for text beautification, it represents a concern for user experience, a way to make scripts feel more “human”.

Often, when we write code, we focus too much on logic and forget that “aesthetics” and “usability” are equally important.

Programmers can be a bit romantic too~ 🎈

🌸 A thumbs up and a view are the biggest support for Huajie

Leave a Comment