Python Forum
Color a table cell based on specific text - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Color a table cell based on specific text (/thread-40372.html)

Pages: 1 2


Color a table cell based on specific text - Creepy - Jul-19-2023

Hi Team,

Am trying to find syntax for html.replace to replace color of the td with specific text, for eg: If text A, color Green, if text B, color Red.

I tried like below but, it doesn't seem to work at all, TABLE:

1234 ABCDEF TEXT A
5678 ABCDEF TEXT B
0987 ABCDFEF TEXT A

html = html.replace("<thead>","<thead style=\"background-color:#00FF00\">")
html2 = html2.replace("<td=TEXT A>","<td style=\"background-color:#00FF00\">")
html3 = html3.replace("<td=TEXT B>","<td style=\"background-color:##FF0000\">")



RE: Color a table cell based on specific text - deanhystad - Jul-19-2023

Please post the code. What is html, html2 and html3?

Does the string replacement work, and the problem is this does not produce the desired result, or is the string replacement?

" is not the only way to quote a string.
html = html.replace("<thead>","""<thead style="background-color:#00FF00">""")
html3 = html3.replace('<td=TEXT B>', '<td style="background-color:##FF0000">')
Shouldn't the replacement string for '<td=TEXT B>' include 'TEXT B' somewhere?


RE: Color a table cell based on specific text - Creepy - Jul-19-2023

Hi,

Am trying to add the table to e-mail body, which is why using html.replace here.
My bad, sorry it should be html for all 3:

html = html.replace("<thead>","<thead style=\"background-color:#00FF00\">")
html = html.replace("<td=TEXT A>","<td style=\"background-color:#00FF00\">")
html = html.replace("<td=TEXT B>","<td style=\"background-color:##FF0000\">")
Does the string replacement work, and the problem is this does not produce the desired result, or is the string replacement?
To answer to your question, it does not produce the desired result.

If I try to use single quote, it does not print/display the table at all in my e-mail body.


RE: Color a table cell based on specific text - deanhystad - Jul-19-2023

Quote:If I try to use single quote, it does not print/display the table at all in my e-mail body.
Odd as it sounds, this probably indicates the replace is working. As I mentioned, your replacement string is removing values from your table. This code removes "<td=TEXT B>", replacing it with "<td style=\"background-color:##FF0000\">"
html = html.replace("<td=TEXT B>","<td style=\"background-color:##FF0000\">")
Don't you need both color an content?


RE: Color a table cell based on specific text - Creepy - Jul-19-2023

Not sure if the table structure is not allowing the color to work?

<table>
<thead>
All header columns here.....
</thead>
<tbody>
<tr><td style="text-align: right "> 1234</td><td style="text-align: center "> ABCDEF</td><td style="text-align: center ">TEXT A</td></tr>
<tr><td style="text-align: right "> 1234</td><td style="text-align: center "> ABCDEF</td><td style="text-align: center ">TEXT B</td></tr>
</tbody>
</table>



RE: Color a table cell based on specific text - Creepy - Jul-19-2023

Also to add, I tested if html.replace, works it does work.

 html = html.replace("Text B","Green)
Output was:
5678 ABCDEF Green

Think some how the color is been stripped of in the output.
Yes, I do need both color and content as well.


RE: Color a table cell based on specific text - deanhystad - Jul-19-2023

I think this would work.
Output:
<tr><td style="text-align: right "> 1234</td><td style="text-align: center"> ABCDEF</td><td style="text-align: center; background-color: #FF0000">TEXT A</td></tr>
This means your find/replace strings need to change.
html = html.replace("\">TEXT A", "; background-color: #00FF00\">TEXT A")
There has got to be a better way to do this, like using beautiful soup.


RE: Color a table cell based on specific text - Creepy - Jul-19-2023

Thanks much, this worked out like charm Smile


RE: Color a table cell based on specific text - Creepy - Jul-20-2023

Hmm..I faced the issue, after couple of testing where, if I have TEXT B, it'll color RED and it doesn't color TEXT A even if the TEXT is present in the table.

TEXT B ( cell: RED )
TEXT B ( cell: RED )

TEXT A ( fails to color green )
Further, I even tried, but when I try to import the table as HTML for e-mail body, it gives out AttributeError: 'str' object has no attribute 'style'

def color(value):
    if value == 'TEXT B':
        color = 'red'
    elif value  == 'TEXT A':
        color = 'green'
    return 'background-color: %s' % color
df.style.applymap(color, subset=['Col3'])
I would need help, where if the table col has TEXT B color red and TEXT A color green.

#html = html.replace("\">TEXT A", "; background-color: #7CFC00\">TEXT A")
#html = html.replace("\">TEXT B", "; background-color: #Ff0000\">TEXT B")



RE: Color a table cell based on specific text - deanhystad - Jul-20-2023

I find your posts frustrating. You don't provide enough information for me to understand what you are talking about.
As an example, what is this supposed to mean? Why is it included in your post?
(Jul-20-2023, 12:16 PM)Creepy Wrote:
TEXT B ( cell: RED )
TEXT B ( cell: RED )

TEXT A ( fails to color green )
The code you post is incomplete. It is best if you can provide code that runs. Failing that, provide enough code to fully define the problem. I don't expect you to provide an email, but you could provide the html for the table or show how you imported the HTML. Looking at the code below I have no idea what "df" is supposed to be (error says it is a string), and because of that I have no hope of answering why you are getting the error.
(Jul-20-2023, 12:16 PM)Creepy Wrote: Further, I even tried, but when I try to import the table as HTML for e-mail body, it gives out AttributeError: 'str' object has no attribute 'style'
def color(value):
    if value == 'TEXT B':
        color = 'red'
    elif value  == 'TEXT A':
        color = 'green'
    return 'background-color: %s' % color
df.style.applymap(color, subset=['Col3'])
And if you have an error, post the entire error and the code that generated the error. Not a snippet of the message and a snippet of the code.

I mentioned that there has to be a better way than using str.replace(). Treating HTML as strings is problematic for many reasons. The str.replace() from my earlier post would not work if there wasn't a "style" already applied to the cell.

If you decide to use str.replace(), don't just say "it doesn't color TEXT A". That is almost no information. Why doesn't it color TEXT A? What does the HTML look like after the replace? Is the problem that the replace results in malformed HTML? Is the problem that the style is not properly formatted? I don't know, and I cannot know from what you have posted.

My guess about the attribute error is that you have an HTML string, You should use a tool that parses the HTML string and creates a roadmap. You would search for table cells that contain the text you are looking for, get the style for that cell, modify the style, and apply.