[英]How to center printed text with pyfiglet in Python
我想在用 py-figlet ( https://github.com/pwaller/pyfiglet ) 制作的中心文本上打印。
我的代碼如下所示:
from pyfiglet import Figlet
f = Figlet(font='ascii___')
def DrawText(text):
return f.renderText(text)
print(DrawText('text')) <- Center it
在輸出時,我希望使用 pyfiglet 打印在中心打印文本。
您可以巧妙地將.center()
與shutil
模塊一起使用:
from pyfiglet import Figlet
import shutil
f = Figlet(font='ascii___')
def DrawText(text,center=True):
if center:
print(*[x.center(shutil.get_terminal_size().columns) for x in f.renderText(text).split("\n")],sep="\n")
else:
print(f.renderText(text))
DrawText('text',center=True)
您可以將關鍵字justify與“auto”、“left”、“center”或“right”一起使用
我這樣做了:
import pyfiglet
txt = "title"
banner = pyfiglet.figlet_format(txt, font="slant", justify="center")
print(banner)
我找不到該模塊的體面文檔。 所以我不得不深入研究代碼,找到 FigletBuilder 類構造函數的關鍵字。 它是這樣開始的:
class FigletBuilder(object):
"""
Represent the internals of the build process
"""
def __init__(self, text, font, direction, width, justify):
self.text = list(map(ord, list(text)))
self.direction = direction
self.width = width
self.font = font
self.justify = justify
main()函數(當前第 865 行)中的OptionParser還給出了如何使用關鍵字的指示,以防萬一您想了解有關如何使用關鍵字參數的更多信息,但不想滾動瀏覽1000行代碼^^
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.