How to wrapping text content when using datafram.to_html()

Question:

There’s text content wrapping in my excel(one grid),just like this:

"2022/10/17: Update detail in 10/18
2022/10/18: Keep tracking 1 week. Check point: 10/25"

when I read it with pandas(pd.read_excel), it becomes:

"2022/10/17: Update detail in 10/18n2022/10/18: Keep tracking 1 week. Check point: 10/25"

so when I transfer the dataframe with df.to_hdml(), (I want to use SMTP to send the content),it becomes:

</thead> 
<tbody>
 <tr> 
  <td>2022/10/17: Update detail in 10/18n2022/10/18: Keep tracking 1 week. Check point: 10/25</td>

The ‘n’ show as text in the content instead of line wrap.
Here I call help to find out solution, to wrap text content in the html td content. thank you in advance.

I’ve created one function:

def wrap_words(word):
    words = word.split('n') 
    word_list =[]     
    for i in range(1,len(words)):
        word_list.append(words[i])
    wording = 'n'.join(word_list)
    return wording

but it doesn’t work.

also
.apply(lambda x: str(x).replace('n','<br />'))
dosen’t work.

Asked By: LynChen

||

Answers:

Simply replace n with <br>Html new line

print(df.to_html().replace("\n","<br>") ) 

Html page table #

enter image description here

Answered By: Bhargav