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
"from colorama import Fore,Back,Style" 后使用Fore、Back、Style的常數+文本來使用:
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(autoreset = True):
init(autoreset = False):
# 如果未設置autoreset=True,需要使用如下代碼重置終端顏色為初始設置
#print(Fore.RESET + Back.RESET + Style.RESET_ALL)
實列如下:

效果:

若要想讓第二行顏色不是之前設置的紅色就需要這樣操作:

效果:

