3.1. ANSI Color
3.1.1. ansicolors
3.1.2. termcolor
from termcolor import colored # then use Termcolor for all colored text output print(colored('Hello, World!', 'green', 'on_red'))
import sys from termcolor import colored, cprint text = colored('Hello, World!', 'red', attrs=['reverse', 'blink']) print(text) cprint('Hello, World!', 'green', 'on_red') print_red_on_cyan = lambda x: cprint(x, 'red', 'on_cyan') print_red_on_cyan('Hello, World!') print_red_on_cyan('Hello, Universe!') for i in range(10): cprint(i, 'magenta', end=' ') cprint("Attention!", 'red', attrs=['bold'], file=sys.stderr)
3.1.3. Colorama
pip install colorama
3.1.3.1. 初始化操作
init(autoreset = False),当 autoreset = True 时自动恢复到默认颜色
#!/usr/bin/env python from colorama import init, Fore, Back, Style if __name__ == "__main__": init(autoreset=True) # 初始化,自动恢复到默认颜色 print(Fore.RED + 'some red text') print(Back.GREEN + 'and with a green background') print(Style.DIM + 'and in dim text')
3.1.3.2. 常用格式
Fore是针对字体颜色,Back是针对字体背景颜色,Style是针对字体格式
set_cols_align(self, array) 设置水平对齐
set_cols_valign(self, array) 设置垂直对齐
from texttable import Texttable table = Texttable() table.set_cols_align(["l", "r", "c"]) table.set_cols_valign(["t", "m", "b"]) table.add_rows([["Name", "Age", "Nickname"], ["Mr\nXavier\nHuon", 32, "Xav'"], ["Mr\nBaptiste\nClement", 1, "Baby"], ["Mme\nLouise\nBourgeau", 28, "Lou\n\nLoue"]]) print(table.draw()) print()
输出结果
+----------+-----+----------+ | Name | Age | Nickname | +==========+=====+==========+ | Mr | | | | Xavier | 32 | | | Huon | | Xav' | +----------+-----+----------+ | Mr | | | | Baptiste | 1 | | | Clement | | Baby | +----------+-----+----------+ | Mme | | Lou | | Louise | 28 | | | Bourgeau | | Loue | +----------+-----+----------+
3.3.2. 设置表格风格
from texttable import Texttable table = Texttable() for header in (Texttable.BORDER, Texttable.HEADER, Texttable.HLINES, Texttable.VLINES): table.set_deco(header) table.set_cols_align(["l", "r", "c"]) table.set_cols_valign(["t", "m", "b"]) table.add_rows([["Name", "Age", "Nickname"], ["Neo", 35, "netkiller"], ["李磊", 23, "Lee"], ["韩美美", 28, "May"]]) print(table.draw()) print("\n\n")
输出结果
+--------------------------+ | Name Age Nickname | | Neo 35 netkiller | | 李磊 23 Lee | | 韩美美 28 May | +--------------------------+ Name Age Nickname ======================== Neo 35 netkiller 李磊 23 Lee 韩美美 28 May Neo 35 netkiller 李磊 23 Lee 韩美美 28 May Name Age Nickname Neo 35 netkiller +--------------------------+ 李磊 23 Lee +--------------------------+ 韩美美 28 May +--------------------------+ Neo 35 netkiller +--------------------------+ 李磊 23 Lee +--------------------------+ 韩美美 28 May +--------------------------+ Neo 35 netkiller +--------------------------+ 李磊 23 Lee +--------------------------+ 韩美美 28 May Name | Age | Nickname Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May Neo | 35 | netkiller 李磊 | 23 | Lee 韩美美 | 28 | May
3.3.3. 自定义风格
自定义行列线条字符
set_chars(self, array) | Set the characters used to draw line