Python Forum
Color a table cell based on specific text
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Color a table cell based on specific text
#1
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\">")
Reply
#2
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?
Reply
#3
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.
Reply
#4
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?
Reply
#5
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>
Reply
#6
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.
Reply
#7
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.
Reply
#8
Thanks much, this worked out like charm Smile
Reply
#9
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")
Reply
#10
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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,040 Oct-25-2023, 09:09 AM
Last Post: codelab
  Deleting rows based on cell value in Excel azizrasul 11 2,677 Oct-19-2022, 02:38 AM
Last Post: azizrasul
  Use module docx to get text from a file with a table Pedroski55 8 6,201 Aug-30-2022, 10:52 PM
Last Post: Pedroski55
  select Eof extension files based on text list of filenames with if condition RolanRoll 1 1,532 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,042 Mar-28-2022, 03:38 PM
Last Post: snippsat
  How to perform DESC table sort on dates stored as TEXT type. hammer 7 2,235 Mar-15-2022, 01:10 PM
Last Post: hammer
  How to find tags using specific text (timestamps) in a url? q988988 1 1,384 Mar-08-2022, 08:09 AM
Last Post: buran
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,652 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  Extract text based on postion and pattern guddu_12 2 1,644 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Extract specific sentences from text file Bubly 3 3,423 May-31-2021, 06:55 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020