talira

termcolor

ANSI colour formatting for terminal output in Python

open sourceMITfree

termcolor is a small Python library for colouring terminal output with ANSI codes. colored() returns coloured text and cprint() prints it, with named text colours, background highlights, attributes such as bold, underline and reverse, and arbitrary RGB colours as tuples. Colour can be turned off or forced per call, or through the ANSI_COLORS_DISABLED, NO_COLOR and FORCE_COLOR environment variables, and a dumb terminal disables it; otherwise it detects whether the terminal supports colour.

Problems it solves

  • print coloured text to the terminal from a Python script
  • highlight errors in red in my Python CLI output
  • make my Python tool respect NO_COLOR

Features

  • colored() and cprint() with named colours, highlights and attributes
  • RGB colours as 0-255 tuples
  • honours NO_COLOR, FORCE_COLOR and ANSI_COLORS_DISABLED
  • detects whether the terminal supports colour

Limitations

  • Attributes such as bold, blink and italic depend on the terminal; on Windows only reverse is listed as working.1

Alternatives

The same job

These do what termcolor does.

  1. Colorama Cross-platform coloured terminal text in Python

Compare termcolor with the top 3