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
#12
I don't see what you are seeing.
html = """
<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 B</td></tr>
<tr><td style="text-align: right "> 4567</td><td style="text-align: center "> XYZE</td><td style="text-align: center ">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>
"""

html = html.replace("\">TEXT A", "; background-color: #00FF00\">TEXT A")
html = html.replace("\">TEXT B", "; background-color: #FF0000\">TEXT B")

print(html)
Output:
<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; background-color: #00FF00">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: #00FF00">TEXT A</td></tr> </tbody> </table>
I think using str.repace() is going to error prone, but it doesn't appear to be so here.
Reply


Messages In This Thread
RE: Color a table cell based on specific text - by deanhystad - Jul-27-2023, 02:48 PM

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