Pure Python implementation of FIGlet for creating ASCII art text from regular text using various fonts
—
Fine-grained control over ASCII art text rendering including direction, justification, width constraints, and font management. The Figlet class provides the primary interface for advanced rendering scenarios.
Main rendering class that provides comprehensive control over ASCII art generation with persistent configuration.
class Figlet:
def __init__(self, font: str = "standard", direction: str = "auto", justify: str = "auto", width: int = 80):
"""
Initialize Figlet renderer with rendering options.
Parameters:
- font (str): Font name to use for rendering (default: "standard")
- direction (str): Text direction - "auto", "left-to-right", "right-to-left"
- justify (str): Text justification - "auto", "left", "center", "right"
- width (int): Maximum output width in characters (default: 80)
Raises:
FontNotFound: If the specified font cannot be located
FontError: If there is a problem parsing the font file
"""Primary method for converting text to ASCII art using the configured settings.
def renderText(self, text: str) -> FigletString:
"""
Render text as ASCII art using current font and settings.
Parameters:
- text (str): Text to convert to ASCII art
Returns:
FigletString: ASCII art representation with manipulation methods
Raises:
CharNotPrinted: If width is insufficient to print a character
FontError: If font rendering encounters an error
"""Change the active font while preserving other rendering settings.
def setFont(self, **kwargs: str) -> None:
"""
Change the active font and reload font data.
Parameters:
- font (str): New font name to load
Raises:
FontNotFound: If the specified font cannot be located
FontError: If there is a problem parsing the font file
"""Retrieve available fonts using the current Figlet instance.
def getFonts(self) -> list[str]:
"""
Get list of all available font names.
Returns:
list[str]: Sorted list of available font names
"""from pyfiglet import Figlet
# Create renderer with default settings
fig = Figlet()
result = fig.renderText("Hello World")
print(result)
# Create renderer with custom settings
fig = Figlet(font="slant", width=120, justify="center")
result = fig.renderText("Centered Text")
print(result)from pyfiglet import Figlet
fig = Figlet(width=100)
# Try different fonts for the same text
fonts = ["standard", "big", "slant", "shadow"]
text = "PyFiglet"
for font_name in fonts:
try:
fig.setFont(font=font_name)
result = fig.renderText(text)
print(f"\n--- {font_name.upper()} ---")
print(result)
except Exception as e:
print(f"Error with {font_name}: {e}")from pyfiglet import Figlet
# Right-to-left text with right justification
fig = Figlet(
font="standard",
direction="right-to-left",
justify="right",
width=60
)
# Render multiple lines
texts = ["Line 1", "Line 2", "Line 3"]
for text in texts:
result = fig.renderText(text)
print(result)from pyfiglet import Figlet
fig = Figlet()
# Get available fonts
fonts = fig.getFonts()
print(f"Total fonts available: {len(fonts)}")
# Test fonts with sample text
sample_text = "Test"
for font in fonts[:5]: # Test first 5 fonts
try:
fig.setFont(font=font)
result = fig.renderText(sample_text)
print(f"\n{font}:")
print(result.strip())
except Exception as e:
print(f"{font}: {e}")The Figlet class provides computed properties for accessing current settings:
fig = Figlet(direction="auto", justify="auto")
# These properties compute actual values based on font and settings
actual_direction = fig.direction # e.g., "left-to-right"
actual_justify = fig.justify # e.g., "left"Advanced rendering can encounter several error conditions:
Handle these appropriately for robust applications:
from pyfiglet import Figlet, FontNotFound, CharNotPrinted
try:
fig = Figlet(font="nonexistent", width=20)
result = fig.renderText("Wide Text That Won't Fit")
except FontNotFound:
print("Font not available, using default")
fig = Figlet(width=20)
result = fig.renderText("Text")
except CharNotPrinted:
print("Text too wide, increasing width")
fig = Figlet(width=80)
result = fig.renderText("Text")Install with Tessl CLI
npx tessl i tessl/pypi-pyfiglet