3

Is it possible to print out things in different colors in Python for Windows? I already enabled ANSI.sys, but this does not seam to work.

I want to be able to print one line in red, and the next in green, etc.

Deduplicator
  • 44,692
  • 7
  • 66
  • 118
John Howard
  • 61,037
  • 23
  • 50
  • 66
  • 1
    But are you putting in the ANSI escape sequences for color, such as `ESC ] 32` for green, just before the text to be colored? If so, what happens if you write the same escape sequence and text to a file and put it on the console with the `TYPE` command? – Alex Martelli Aug 18 '10 at 04:48

1 Answers1

2

The WConio module should be all you need to accomplish this.

WConio.textbackground(color) sets the background color without changing the foreground. See below for the color constants.

WConio.textcolor(color) sets the foreground color without changing the background. See below for the color constants.

The constants it refers to are not actually listed on the page, but are at the top of the WConio.py file:

BLACK = 0
BLUE = 1
GREEN = 2
CYAN = 3
RED = 4
MAGENTA = 5
BROWN = 6
LIGHTGRAY = LIGHTGREY = 7
DARKGRAY = DARKGREY = 8
LIGHTBLUE = 9
LIGHTGREEN = 10
LIGHTCYAN = 11
LIGHTRED = 12
LIGHTMAGENTA = 13
YELLOW = 14
WHITE = 15

So a full call to set the text foreground colour to red would be:

WConio.textcolor(WConio.RED)
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Andrew
  • 12,821
  • 2
  • 26
  • 18