Jul-20-2023, 04:03 PM
(This post was last modified: Jul-20-2023, 04:03 PM by deanhystad.)
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?
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.
As an example, what is this supposed to mean? Why is it included in your post?
(Jul-20-2023, 12:16 PM)Creepy Wrote: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.TEXT B ( cell: RED ) TEXT B ( cell: RED ) TEXT A ( fails to color green )
(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'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.
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 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.