I am attempting to create a colorful table in Python using the tabulate
and colorama
libraries. However, despite having both libraries installed and confirmed that colorama
works fine with other programs, the output is not colored as expected.
Here's the code I'm using:
from tabulate import tabulate
import colorama
from colorama import Fore, Style
# Initialize colorama to work on Windows
colorama.init(autoreset=True)
# Sample data for the table
data = [
["Name", "Age", "Country"],
["John", 30, "USA"],
["Alice", 25, "Canada"],
["Bob", 22, "UK"],
]
# Table headers and data, aligned center and colored
table = tabulate(data, headers="firstrow", tablefmt="grid")
colored_table = f"{Fore.CYAN}{Style.BRIGHT}{table}{Style.RESET_ALL}"
print(colored_table)
However, the output of this code is:
As you can see, the table is displayed, but the colors are not applied. I have double-checked that the colorama
library is functioning correctly with other programs, so the issue seems to be specific to this code.
Can someone kindly identify the potential cause of this problem or provide any solutions to get the colorama
library to work in conjunction with tabulate
for displaying colored output in the table?