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
#11
Sorry for the inconvenience caused here.

Let me explain further as much as I can:

Below is how my table is structured:

<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 "> 4567</td><td style="text-align: center "> XYZE</td><td style="text-align: center ">TEXT A</td></tr>
<tr><td style="text-align: right "> 8976</td><td style="text-align: center "> FGRHEH</td><td style="text-align: center ">TEXT B</td></tr>
<tr><td style="text-align: right ">2354</td><td style="text-align: center ">JKHJ</td><td style="text-align: center ">TEXT B</td></tr>
</tbody>
</table>
The python replace code for color coding:

html = html.replace("\">TEXT A", "; background-color: #7CFC00\">TEXT A")
html = html.replace("\">TEXT B", "; background-color: #Ff0000\">TEXT B")
But problem I see is, if my table contains only TEXT A, it'll color green ( Since the column in table only contains TEXT A )

<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 ; background-color: #7CFC00">TEXT A</td></tr>
<tr><td style="text-align: right "> 4567</td><td style="text-align: center "> XYZE</td><td style="text-align: center ; background-color: #7CFC00">TEXT A</td></tr>
<tr><td style="text-align: right "> 8976</td><td style="text-align: center "> FGRHEH</td><td style="text-align: center ; background-color: #7CFC00"">TEXT A</td></tr>
<tr><td style="text-align: right ">2354</td><td style="text-align: center ">JKHJ</td><td style="text-align: center ; background-color: #7CFC00"">TEXT A</td></tr>
</tbody>
</table>
If the column in table contains both TEXT A and TEXT B, and say TEXT B is the 1st entry in the table, it only color RED and not color GREEN for TEXT A.

<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 ; background-color: #Ff0000">TEXT B</td></tr>
<tr><td style="text-align: right "> 4567</td><td style="text-align: center "> XYZE</td><td style="text-align: center ; background-color: #Ff0000">TEXT B</td></tr>
<tr><td style="text-align: right "> 8976</td><td style="text-align: center "> FGRHEH</td><td style="text-align: center">TEXT A</td></tr>
<tr><td style="text-align: right ">2354</td><td style="text-align: center ">JKHJ</td><td style="text-align: center">TEXT A</td></tr>
</tbody>
</table>
Reply


Messages In This Thread
RE: Color a table cell based on specific text - by Creepy - Jul-27-2023, 01:30 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Turtle Star Fill Color Yellow-White Interchanging Color Effect codelab 9 1,223 Oct-25-2023, 09:09 AM
Last Post: codelab
  Deleting rows based on cell value in Excel azizrasul 11 2,893 Oct-19-2022, 02:38 AM
Last Post: azizrasul
  Use module docx to get text from a file with a table Pedroski55 8 6,620 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,603 Apr-04-2022, 09:29 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,203 Mar-28-2022, 03:38 PM
Last Post: snippsat
  How to perform DESC table sort on dates stored as TEXT type. hammer 7 2,347 Mar-15-2022, 01:10 PM
Last Post: hammer
  How to find tags using specific text (timestamps) in a url? q988988 1 1,436 Mar-08-2022, 08:09 AM
Last Post: buran
  Sum the values in a pandas pivot table specific columns klllmmm 1 4,776 Nov-19-2021, 04:43 PM
Last Post: klllmmm
  Extract text based on postion and pattern guddu_12 2 1,720 Sep-27-2021, 08:32 PM
Last Post: guddu_12
  Extract specific sentences from text file Bubly 3 3,529 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