colorama是一個python專門用來在控制台、命令行輸出彩色文字的模塊,可以跨平台使用。
1. 安裝colorama模塊
pip install colorama
可用格式常數:
Fore: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Back: BLACK, RED, GREEN, YELLOW, BLUE, MAGENTA, CYAN, WHITE, RESET. Style: DIM, NORMAL, BRIGHT, RESET_ALL
跨平台印刷彩色文本可以使用彩色光的常數簡稱ANSI轉義序列:
from colorama import Fore,Back,Style
print (Fore.RED + "some red text")
print (Back.GREEN + "and with a green background")
print (Style.DIM + "and in dim text")
print (Style.RESET_ALL)
print ("back to normal now!!")
Init關鍵字參數:
init()接受一些* * kwargs覆蓋缺省行為
init(autoreset = False):
如果你發現自己一再發送重置序列結束時關閉顏色變化每一個打印,然后init(autoreset = True)將自動化
示例:
from colorama import init,Fore
init(autoreset=True)
print (Fore.RED + "welcome to python !!")
print ("automatically back to default color again")
