20
Help us understand the problem. What are the problem?

More than 5 years have passed since last update.

posted at

updated at

Python colorama 色メモ(Cmder環境)

はじめに

Windows7 Cmder上でPythonの文字色指定ライブラリcoloramaを使った時の色についてメモします。

使い方

初期化

import colorama
from colorama import Fore, Back, Style

colorama.init(autoreset=True)

文字色指定

文字出力時に指定して、以降の文字に色をつけます。

print("foo," + Fore.BLUE + "bar," + Back.RED + "baz")

image

色ついた!

指定できる属性群

以下が使えます。RESETを除けば16色。

Fore.BLACK           Fore.LIGHTBLACK_EX   Fore.LIGHTMAGENTA_EX Fore.MAGENTA         Fore.YELLOW
Fore.BLUE            Fore.LIGHTBLUE_EX    Fore.LIGHTRED_EX     Fore.RED
Fore.CYAN            Fore.LIGHTCYAN_EX    Fore.LIGHTWHITE_EX   Fore.RESET
Fore.GREEN           Fore.LIGHTGREEN_EX   Fore.LIGHTYELLOW_EX  Fore.WHITE

Back.BLACK           Back.LIGHTBLACK_EX   Back.LIGHTMAGENTA_EX Back.MAGENTA         Back.YELLOW
Back.BLUE            Back.LIGHTBLUE_EX    Back.LIGHTRED_EX     Back.RED
Back.CYAN            Back.LIGHTCYAN_EX    Back.LIGHTWHITE_EX   Back.RESET
Back.GREEN           Back.LIGHTGREEN_EX   Back.LIGHTYELLOW_EX  Back.WHITE

Style.BRIGHT    Style.DIM       Style.NORMAL    Style.RESET_ALL

サンプル

カラフル!
image

スタイルは地味。DIMとNORMALは違いなかった。
image

おまけ

上記の色サンプル並べるスニペット。

# 色
for attr in dir(Fore):
    if attr[0] != '_':
        print(getattr(Fore, attr) + "### Fore.{:<15}".format(attr) + Back.WHITE + "### Fore.{:<15}".format(attr))
        print(getattr(Back, attr) + "### Back.{:<15}".format(attr) + Fore.BLACK + "### Back.{:<15}".format(attr))

# スタイル
for attr in dir(Style):
    if attr[0] != '_':
        print(getattr(Style, attr) + "### Style.{:<10} ###".format(attr))
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Sign upLogin
20
Help us understand the problem. What are the problem?