Introduction
If you are a Linux or Mac user, you may have heard of or used a software tool called figlet. It prints ASCII text banners of a given plain text. Our beloved Python language also offers a module called pyfiglet that does the same thing, printing stylish ASCII text using ASCII characters.
pyfiglet offers a total of 419 font types which allow us to print beautiful text banners on the Terminal(Command Prompt for Windows). There is another useful module named termcolor. We can print the same text but coloring by combining termcolor with pyfiglet module.
We will discuss the usage of those two modules in this tutorial. Lastly, I'll share a cool Python Program that I created using these two nice tools. This program continuously prints any greeting message with a person's name in ASCII Text with random font types and colors to the terminal or command prompt.
As an example, I will use this Python program to print a "Happy Birthday" message, continuously. It will appear as raining text on the screen.
👉Visit Also: Draw the Sketch of Lionel Messi using a Python Program
Requirements
Before you start writing your code just take a break here. Install these necessary modules if you didn't already.
Use 'pip3' instead of 'pip' for Linux.
☛Install pyfiglet: pip install pyfiglet==0.7
☛Install termcolor: pip install termcolor
Tutorial - pyfiglet
Here we will cover just three examples. The number is enough to understand how to use the pyfiglet module to print something stylish on the terminal.
Example 1 - Print the text with the default font type
In this case, the above-mentioned task will be done in just three lines of python code. Let's see it.
import pyfiglet as pf
textArt = pf.figlet_format("PySeek")
print(textArt)
Output
Example 2 - Mention the font type
In the previous example, we didn't mention the font type, so the program used the default font. But, as I mentioned earlier, pyfiglet offers a total of 419 different font types and the number is huge.
The code will be the same as before but an additional parameter must be specified in the figlet_format() function.
Here, I will mention only one font type, the rest of the 418 font types you will get at the end of this tutorial.
import pyfiglet as pf
textArt = pf.figlet_format("PySeek", font="ascii___")
print(textArt)
Output
Example 3 - Mention the direction
You can specify the direction in the program according to which the given text will be printed. There are three choices, 'auto', 'left-to-right', and 'right-to-left'. For the last option, you have to put your text in reverse order, otherwise, the program will print the stylist ASCII text in reverse.
import pyfiglet as pf
textArt = pf.figlet_format("keeSyP", direction='right-to-left')
print(textArt)
Output
Tutorial - termcolor
We will briefly look at how to use the termcolor module together with pyfiglet. If you are interested in learning more about termcolor, this Project Description may help you.
This module offers a total of 8 colors('grey', 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'white') to work with. I will pick one of them as an example('yellow'). Let's see the code.
Code
The code is pretty straightforward like the rest. You just need to specify the font type, the text you want to print, and the color of the output text Art.
from pyfiglet import Figlet
from termcolor import colored
F = Figlet(font="ascii___")
textArt = colored(F.renderText('PySeek'), 'yellow')
print("\n", textArt)
Output
The Application Program
Try to remember, very first I said a program I developed which would continuously print any message entered by us in terminal or command prompt as ASCII text art in different styles with different colors for a long time.
For example, here I will print a "Happy Birthday" message with a Person's Name. The output will look like the text is raining down on the screen. It will provide a nice animation. With this program, you can wish to your girlfriend or friend, or any other loved one by displaying your message as random ASCII text.
Watch the full video to better understand how the program actually works.
The program randomly selects a font from 419 font types and does the same for color selection. I hope the examples we did at the beginning will help you understand the logic of this program.
Create a separate folder for this project and the fonts.txt file must be present there along with this wish.py program file.
wish.py
import time
import time
import random
import pyfiglet as pf
from pyfiglet import Figlet
from termcolor import colored
# Write your message here
Text = "Happy Birthday"
# Enter his/her name
Name = "Alicia"
# Reverse your given name
reverseText = Name[::-1]
colorList = ['red', 'green', 'yellow', 'blue']
timeInterval = [0.2, 0.3, 0.2, 0.4]
# Getting all the font types from 'font.txt'
# and storing into a list
#==================================
dataList = list()
with open('fonts.txt') as f:
for line in f:
dataList.append(line.strip())
#==================================
# This part of the code prints the message
# with different font types(fixed and randomly)
#==================================
for i in range(1,1000):
if(i%10==0):
textArt = pf.figlet_format(Text)
print("\n", textArt)
elif(i%9 == 0):
textArt = pf.figlet_format(Text, font="xsbook")
print(textArt)
elif(i%5==0):
F = Figlet(font=random.choice(dataList))
textArt = colored(F.renderText(Name), random.choice(colorList))
print("\n", textArt)
elif(i%8==0):
F = Figlet(font=random.choice(dataList))
textArt = colored(F.renderText(Text), random.choice(colorList))
print("\n", textArt)
elif(i%7==0):
textArt = pf.figlet_format(Text, font=random.choice(dataList))
print("\n", textArt)
elif(i%4==0):
textArt = pf.figlet_format(reverseText, direction = "right-to-left")
print("\n", textArt)
else:
print("")
time.sleep(random.choice(timeInterval))
#==================================
fonts.txt
1943____
3-d
3x5
4x4_offr
5lineoblique
5x7
5x8
64f1____
6x10
6x9
a_zooloo
acrobatic
advenger
alligator
alligator2
alphabet
aquaplan
asc_____
ascii___
assalt_m
asslt__m
atc_____
atc_gran
avatar
b_m__200
banner
banner3
banner3-D
banner4
barbwire
basic
battle_s
battlesh
baz__bil
beer_pub
bell
big
bigchief
binary
block
brite
briteb
britebi
britei
broadway
bubble
bubble__
bubble_b
bulbhead
c1______
c2______
c_ascii_
c_consen
calgphy2
caligraphy
catwalk
caus_in_
char1___
char2___
char3___
char4___
charact1
charact2
charact3
charact4
charact5
charact6
characte
charset_
chartr
chartri
chunky
clb6x10
clb8x10
clb8x8
cli8x8
clr4x6
clr5x10
clr5x6
clr5x8
clr6x10
clr6x6
clr6x8
clr7x10
clr7x8
clr8x10
clr8x8
coil_cop
coinstak
colossal
com_sen_
computer
contessa
contrast
convoy__
cosmic
cosmike
cour
courb
courbi
couri
crawford
cricket
cursive
cyberlarge
cybermedium
cybersmall
d_dragon
dcs_bfmo
decimal
deep_str
demo_1__
demo_2__
demo_m__
devilish
diamond
digital
doh
doom
dotmatrix
double
drpepper
druid___
dwhistled
e__fist_
ebbs_1__
ebbs_2__
eca_____
eftichess
eftifont
eftipiti
eftirobot
eftitalic
eftiwall
eftiwater
epic
etcrvs__
f15_____
faces_of
fair_mea
fairligh
fantasy_
fbr12___
fbr1____
fbr2____
fbr_stri
fbr_tilt
fender
finalass
fireing_
flyn_sh
fourtops
fp1_____
fp2_____
fraktur
funky_dr
future_1
future_2
future_3
future_4
future_5
future_6
future_7
future_8
fuzzy
gauntlet
georgia11
ghost_bo
goofy
gothic
gothic__
graceful
gradient
graffiti
grand_pr
green_be
hades___
heavy_me
helv
helvb
helvbi
helvi
heroboti
hex
high_noo
hills___
hollywood
home_pak
house_of
hypa_bal
hyper___
inc_raw_
invita
isometric1
isometric2
isometric3
isometric4
italic
italics_
ivrit
jazmine
jerusalem
joust___
katakana
kban
kgames_i
kik_star
krak_out
larry3d
lazy_jon
lcd
lean
letter_w
letters
letterw3
lexible_
linux
lockergnome
mad_nurs
madrid
magic_ma
marquee
master_o
maxfour
mayhem_d
mcg_____
mig_ally
mike
mini
mirror
mnemonic
modern__
morse
moscow
mshebrew210
nancyj
nancyj-fancy
nancyj-underlined
new_asci
nfi1____
nipples
notie_ca
npn_____
ntgreek
nvscript
o8
octal
odel_lak
ogre
ok_beer_
os2
outrun__
p_s_h_m_
p_skateb
pacos_pe
panther_
pawn_ins
pawp
peaks
pebbles
pepper
phonix__
platoon2
platoon_
pod_____
poison
puffy
pyramid
r2-d2___
rad_____
rad_phan
radical_
rainbow_
rally_s2
rally_sp
rampage_
rastan__
raw_recu
rci_____
rectangles
relief
relief2
rev
ripper!_
road_rai
rockbox_
rok_____
roman
roman___
rot13
rounded
rowancap
rozzo
runic
runyc
sans
sansb
sansbi
sansi
sblood
sbook
sbookb
sbookbi
sbooki
script
script__
serifcap
shadow
short
skate_ro
skateord
skateroc
sketch_s
slant
slide
slscript
sm______
small
smisome1
smkeyboard
smscript
smshadow
smslant
smtengwar
space_op
spc_demo
speed
stacey
stampatello
standard
star_war
starwars
stealth_
stellar
stencil1
stencil2
stop
straight
street_s
subteran
super_te
t__of_ap
tanja
tav1____
taxi____
tec1____
tec_7000
tecrvs__
tengwar
term
thick
thin
threepoint
ti_pan__
ticks
ticksslant
times
timesofl
tinker-toy
tomahawk
tombstone
top_duck
trashman
trek
triad_st
ts1_____
tsalagi
tsm_____
tsn_base
tty
ttyb
twin_cob
twopoint
type_set
ucf_fan_
ugalympi
unarmed_
univers
usa_____
usa_pq__
usaflag
utopia
utopiab
utopiabi
utopiai
vortron_
war_of_w
weird
whimsy
xbrite
xbriteb
xbritebi
xbritei
xchartr
xchartri
xcour
xcourb
xcourbi
xcouri
xhelv
xhelvb
xhelvbi
xhelvi
xsans
xsansb
xsansbi
xsansi
xsbook
xsbookb
xsbookbi
xsbooki
xtimes
xtty
xttyb
yie-ar__
yie_ar_k
z-pilot_
zig_zag_
zone7___
👉Visit Also: Create a Time Wasting Websites Blocker using Python
Summary
In this tutorial, we learned how to print color and non-color ASCII text using pyfiglet and termcolor module. In closing, I created an application program using these two nice tools in Python that continuously prints the person's name along with any greeting message to the terminal or command prompt.
For example, here I have printed a "Happy Birthday" message with a Person's Name. You can print anything you want. The code is pretty straightforward. Hope you will understand once you see it.
That's all for today's lesson. If you have any doubts related to this topic, let me know in the comment section. You'll get an immediate response.
Stay tuned with PySeek to get more Lovely Topics.
Thanks for reading!💙 Have a nice day ahead!!
PySeek