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
#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


Messages In This Thread
RE: Color a table cell based on specific text - by deanhystad - Jul-20-2023, 04:03 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,615 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