23
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Python colorama 色メモ(Cmder環境)

Last updated at Posted at 2016-06-11

はじめに

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))
23
20
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?