How to convert colored text to plain text with termcolor?

So, I have a function that returns colored text, but I want to convert the colored text back to plain, white text.

Repl link:

pMove = "Placed " + c(piece, "white") + " at (" + pos[0] + ", " + str(pos[1]) + ")"

I tried just setting the text color to white, but it didn’t work.

You have to strip away the ANSI escape codes because the termcolor library does not provide a direct way to remove color.

For example, you can create a simple function that does this like

import re

def remove_color(text):
    ansi_escape = re.compile(r'\x1B\[[0-?]*[ -/]*[@-~]') #I looked it up and the library uses ANSI codes internally so I believe this is the right re.compile
    return ansi_escape.sub('', text)

And if you still want to convert the pMove string back to plain text after coloring it, you can do it like this:

pMove = "Placed " + c(piece, "white") + " at (" + pos[0] + ", " + str(pos[1]) + ")"
pMove_plain = remove_color(pMove)  # Convert to plain text

# Use pMove_plain wherever you need the plain text version

Ps.: this function only removes the color from the text. It does not change the text itself. So, wherever you use remove_color, the text will appear without color formatting.

1 Like

Thanks! I’ll test to see if that works.

Thanks! The code that you put worked!

Is there a way to keep the bold part? For example,

piece = remove_color(piece)

The piece in this case is a bold = rather than a normal one. (That’s how I differentiate between different pieces)
When I run that code, the value of piece is just = instead of =.

There ins’t a way to just let the bold part (if there is I really don’t know how) but you can remove the color and let the formatting (bold, italic, underline, etc).
The code I gave you remove everything, this code removes only the color and let any formatting in the word. So be aware that any italic, underline, bold and things like that will remain.

import re

def remove_color_but_keep_bold(text):
    ansi_color_escape = re.compile(r'\x1B\[[0-?]*m')
    return ansi_color_escape.sub('', text)

You can use this function in the same way as before:

bold_colored_piece = c("=", "green", attrs=["bold"])
bold_plain_piece = remove_color_but_keep_bold(bold_colored_piece)

I tested the code, but the output from the remove_color_but_keep_bold wasn’t bold when the input was bold.

I’m wondering if there is a way to check if the text is bold to keep it as bold when it removes the colour to then put the bold attribute back on the text.
For example:

if text_is_bold:
    text = remove_color(text)
    text = c(text, attrs=[“bold”])
    text = remove_color(text)

Did you use the function the same way as I’ve put it there?

I tested the code before I gave you and it worked

1 Like

I tried directly copy-pasting the code you suggested into the beginning of my code to see the result, but the output was still nevertheless not bold.

1 Like

I forked your project and tried to fix it.

See if you can copy this part:

And tell me if it works

1 Like

I checked it, but the result remains in plain, not bold text.

Try to change only this part:

def remove_color_but_keep_bold(text):
    ansi_color_escape = re.compile(r'\x1B\[(;?\d+)*m')
    return ansi_color_escape.sub('', text)
1 Like

I forked the project you made to see if it would work, but the result is still that the text isn’t bold

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.