How to set col_width when send smtp mail with datafram.to_html in the content

Question:

I want to send a dataframe by smtp, used df.to_html, but the col-width not under control.
the param ‘col-space’ dose not work whether pass int or str or list to it.

      content = df_mail.to_html(col_space=[20,20,20,20,20,20,500,20,50,20,500],index=False, border=5, justify='left',na_rep=" ")

if i remove the last col(url in it), it becomes much more better.

So i wonder how can i narrow the last col, and widen someone col else.thank you for your kindly advise.enter image description here

Asked By: LynChen

||

Answers:

Outlook will change col_width when displaying e-mail to keep the website link column with max-width, so we need to transfer HTTP links to URLs.
I made a function to accomplish this by doing the following: replace td with td/a can transfer http link to url.
replace_function(df.to_html())

Here’s my the function:

def td_replace(html):
   list_tdPartial = re.findall('<td>http(.*?)</td>', html, re.S)
   list_tdAll = []
   for td in list_tdPartial:
      td = "http" + td
      list_tdAll.append(td)
   for td in list_tdAll:
      list_link = td.split('<br>')
      list_link_after = []
      for i in range(len(list_link)):
         before = list_link[i]
         after = f'<a href="{before}" target="_blank">{cat}{i + 1}</a>' 
         list_link_after.append(after)
      td_after = '<br>'.join(list_link_after)
      html = html.replace(td, td_after)
   return html
Answered By: LynChen
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.