http://blog.csdn.net/pipisorry/article/details/42291265
注:亲测对linux、windows都有效
1.class colors:
reset='\033[0m' bold='\033[01m' disable='\033[02m' underline='\033[04m' reverse='\033[07m' strikethrough='\033[09m' invisible='\033[08m'class fg: black='\033[30m' red='\033[31m' green='\033[32m' orange='\033[33m' blue='\033[34m' purple='\033[35m' cyan='\033[36m' lightgrey='\033[37m' darkgrey='\033[90m' lightred='\033[91m' lightgreen='\033[92m' yellow='\033[93m' lightblue='\033[94m' pink='\033[95m' lightcyan='\033[96m' class bg: black='\033[40m' red='\033[41m' green='\033[42m' orange='\033[43m' blue='\033[44m' purple='\033[45m' cyan='\033[46m' lightgrey='\033[47m'
To use code like this, you can do something like
print bcolors.WARNING + "Warning: No active frommets remain. Continue?"
+ bcolors.ENDC
This will work on unixes including OS X, linux and windows (provided you
enable ansi.sys). There are ansi codes for setting the color, moving the cursor, and more.
{To load ANSI.SYS, add "device=c:\winnt\system32\ansi.sys" tothe CONFIG.NT file in the Windows NT SYSTEM32 directory.Once ANSI.SYS is loaded, you can use any MS-DOS-based program thatmakes use of this driver. However, if you want to use ANSI.SYS tochange the look of the command prompt, you may have to perform someadditional steps.}
format table:
def print_format_table():
"""
prints table of formatted text format options
"""
for style in xrange(8):
for fg in xrange(30,38):
s1 = ''
for bg in xrange(40,48):
format = ';'.join([str(style), str(fg), str(bg)])
s1 += '\x1b[%sm %s \x1b[0m' % (format, format)
print s1
print '\n'
print_format_table()
2.1 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)2.2
from termcolor import colored
print colored('hello', 'red'), colored('world', 'green')
【https://pypi.python.org/pypi/termcolor/】
3.python blessings model
4.python colorama model
5.my settings:
#!/usr/bin/env python # -*- coding: utf-8 -*- """ __title__ = 'color settings' __author__ = 'pi' __mtime__ = '12/30/2014-030' """ """ 将此文件放入python3.4.2\Lib文件夹中 To use code like this, you can do something like: from Colors import * print(REDH,"it's red highlight",'\n', RED,"it's red") print(GREENH,"it's green highlight\n", GREEN,"it's green") print(WHITEH,"it's white highlight"*5,'\n', WHITE,"it's white"*5) 调用输出字体颜色 随时通过修改project>external lib>python3.4.2>lib>colors增删颜色值 color设置格式说明: color = \033[code;前景色;背景色m code: 0 off 1 高亮显示 4 underline 5 闪烁 7 反白显示 8 不可见 前景 背景 颜色 30 40 黑色 31 41 红色 32 42 绿色 33 43 黄色 34 44 蓝色 35 45 紫红色 36 46 青蓝色 37 47 白色 1 1 透明色 """ """ #method 2 class fcolors: RED = '\033[91m' #RED DEFAULT = '\033[0m' #... def disable(self): self.RED = '' self.DEFAULT = '' #... print(fcolors.RED + "Warning: ... Continue?") print(fcolors.DEFAULT) """ #font style DEFAULT = '\033[0m' # DEFAULT = '\033[0;0m' BOLD = '033[1m' DISABLE = '\033[02m' UNDERLINE = '\033[04m' REVERSE = '\033[07m' STRIKETHROUGH = '\033[09m' INVISIBLE = '\033[08m' #light color DARKGREY = '\033[90m' REDL = '\033[91m' #<=> '\033[91;1m' <=> '\033[1;91;1m' lightred GREENL = '\033[0;92;1m' YELLOWL = '\033[0;93;1m' BLUEL = '\033[0;94;1m' PINKL = '\033[0;95;1m' WHITEL = '\033[0;97m' #? #highlight light color REDHL = '\033[1;91m' GREENHL = '\033[1;92;1m' YELLOWHL = '\033[1;93;1m' BLUEHL = '\033[1;94;1m' PINKHL = '\033[1;95;1m' WHITEHL = '\033[1;97m' # highlight color REDH = '\033[1;31m' #REDH = '\033[1;31;1m' GREENH = '\033[1;32;1m' YELLOWH = '\033[1;33;1m' BLUEH = '\033[1;34;1m' PURPLEH = '\033[1;35;1m' WHITEH = '\033[1;37m' # color BLACK ='\033[30m' RED = '\033[0;31;1m' GREEN = '\033[0;32;1m' ORANGE = '\033[0;33;1m' BLUE = '\033[0;34;1m' PURPLE = '\033[0;35;1m' CYAN = '\033[36m' LIGHTGREY = '\033[37m'
from:blog.csdn.net/pipisorry/article/details/42291265
ref:[Print in terminal with colors using Python?]