Home > Net >  Python styled dataframe to email using win32com outlook
Python styled dataframe to email using win32com outlook

Time:12-15

Below is the output of my python code. dataframe is styled and converted to html file.

<style type="text/css">
#T_45521_row0_col3, #T_45521_row0_col4, #T_45521_row0_col5, #T_45521_row0_col6, #T_45521_row0_col8, #T_45521_row0_col9, #T_45521_row0_col10, #T_45521_row1_col3, #T_45521_row1_col4, #T_45521_row1_col5, #T_45521_row1_col6, #T_45521_row1_col8, #T_45521_row1_col9, #T_45521_row1_col10, #T_45521_row2_col3, #T_45521_row2_col4, #T_45521_row2_col5, #T_45521_row2_col6, #T_45521_row2_col8, #T_45521_row2_col9, #T_45521_row2_col10, #T_45521_row3_col3, #T_45521_row3_col4, #T_45521_row3_col5, #T_45521_row3_col6, #T_45521_row3_col8, #T_45521_row3_col9, #T_45521_row3_col10, #T_45521_row4_col3, #T_45521_row4_col4, #T_45521_row4_col5, #T_45521_row4_col6, #T_45521_row4_col8, #T_45521_row4_col9, #T_45521_row4_col10, #T_45521_row4_col11, #T_45521_row5_col3, #T_45521_row5_col4, #T_45521_row5_col5, #T_45521_row5_col6, #T_45521_row5_col8, #T_45521_row5_col9, #T_45521_row5_col10, #T_45521_row6_col3, #T_45521_row6_col4, #T_45521_row6_col5, #T_45521_row6_col6, #T_45521_row6_col8, #T_45521_row6_col9, #T_45521_row6_col10, #T_45521_row7_col3, #T_45521_row7_col4, #T_45521_row7_col5, #T_45521_row7_col6, #T_45521_row7_col8, #T_45521_row7_col9, #T_45521_row7_col10, #T_45521_row8_col3, #T_45521_row8_col4, #T_45521_row8_col5, #T_45521_row8_col6, #T_45521_row8_col8, #T_45521_row8_col9, #T_45521_row8_col10, #T_45521_row9_col3, #T_45521_row9_col4, #T_45521_row9_col5, #T_45521_row9_col6, #T_45521_row9_col8, #T_45521_row9_col9, #T_45521_row9_col10, #T_45521_row9_col11, #T_45521_row10_col3, #T_45521_row10_col4, #T_45521_row10_col5, #T_45521_row10_col6, #T_45521_row10_col8, #T_45521_row10_col9, #T_45521_row10_col10, #T_45521_row11_col3, #T_45521_row11_col4, #T_45521_row11_col5, #T_45521_row11_col6, #T_45521_row11_col8, #T_45521_row11_col9, #T_45521_row11_col10, #T_45521_row12_col3, #T_45521_row12_col4, #T_45521_row12_col5, #T_45521_row12_col6, #T_45521_row12_col8, #T_45521_row12_col9, #T_45521_row12_col10, #T_45521_row13_col3, #T_45521_row13_col4, #T_45521_row13_col5, #T_45521_row13_col6, #T_45521_row13_col8, #T_45521_row13_col9, #T_45521_row13_col10, #T_45521_row14_col3, #T_45521_row14_col4, #T_45521_row14_col5, #T_45521_row14_col6, #T_45521_row14_col8, #T_45521_row14_col9, #T_45521_row14_col10, #T_45521_row15_col3, #T_45521_row15_col4, #T_45521_row15_col5, #T_45521_row15_col6, #T_45521_row15_col8, #T_45521_row15_col9, #T_45521_row15_col10, #T_45521_row16_col3, #T_45521_row16_col4, #T_45521_row16_col5, #T_45521_row16_col6, #T_45521_row16_col8, #T_45521_row16_col9, #T_45521_row16_col10, #T_45521_row17_col3, #T_45521_row17_col4, #T_45521_row17_col5, #T_45521_row17_col6, #T_45521_row17_col8, #T_45521_row17_col9, #T_45521_row17_col10, #T_45521_row18_col3, #T_45521_row18_col4, #T_45521_row18_col5, #T_45521_row18_col6, #T_45521_row18_col8, #T_45521_row18_col9, #T_45521_row18_col10, #T_45521_row19_col3, #T_45521_row19_col4, #T_45521_row19_col5, #T_45521_row19_col6, #T_45521_row19_col8, #T_45521_row19_col9, #T_45521_row19_col10, #T_45521_row19_col11, #T_45521_row20_col3, #T_45521_row20_col4, #T_45521_row20_col5, #T_45521_row20_col6, #T_45521_row20_col8, #T_45521_row20_col9, #T_45521_row20_col10 {
  text-align: right;
  'border-spacing': '10px 50px';
}
#T_45521_row0_col7, #T_45521_row3_col7, #T_45521_row5_col7, #T_45521_row11_col7 {
  background: #0070c0;
  color: white;
  text-align: right;
}
#T_45521_row0_col11, #T_45521_row1_col11, #T_45521_row2_col11, #T_45521_row3_col11, #T_45521_row7_col11, #T_45521_row10_col11, #T_45521_row11_col11, #T_45521_row12_col11, #T_45521_row14_col11, #T_45521_row18_col11 {
  background: #c6efce;
  color: #006100;
  text-align: right;
}
#T_45521_row1_col7, #T_45521_row2_col7, #T_45521_row4_col7, #T_45521_row5_col11, #T_45521_row6_col7, #T_45521_row6_col11, #T_45521_row7_col7, #T_45521_row8_col7, #T_45521_row8_col11, #T_45521_row9_col7, #T_45521_row10_col7, #T_45521_row12_col7, #T_45521_row13_col7, #T_45521_row13_col11, #T_45521_row14_col7, #T_45521_row15_col7, #T_45521_row15_col11, #T_45521_row16_col7, #T_45521_row16_col11, #T_45521_row17_col7, #T_45521_row17_col11, #T_45521_row18_col7, #T_45521_row19_col7, #T_45521_row20_col7, #T_45521_row20_col11 {
  background: #ffbf00;
  text-align: right;
}
</style>
<table id="T_45521_" border="1"  style="border-collapse: collapse">
  <thead>
    <tr>
      <th  >Date</th>
      <th  >Scheme Name</th>
      <th  >Scheme Code</th>
      <th  >Equity Value</th>
      <th  >Total AUM</th>
      <th  >Equity %</th>
      <th  >BM Allocation</th>
      <th  >Effective %</th>
      <th  >Large Cap</th>
      <th  >Mid Cap</th>
      <th  >Small Cap</th>
      <th  >Mid Small</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td id="T_45521_row0_col0"  >14-Dec-2021</td>
      <td id="T_45521_row0_col1"  >ANXX</td>
      <td id="T_45521_row0_col2"  >ANXX</td>
      <td id="T_45521_row0_col3"  >72</td>
      <td id="T_45521_row0_col4"  >1250</td>
      <td id="T_45521_row0_col5"  >5.74</td>
      <td id="T_45521_row0_col6"  >0</td>
      <td id="T_45521_row0_col7"  >0.00</td>
      <td id="T_45521_row0_col8"  >100</td>
      <td id="T_45521_row0_col9"  >0</td>
      <td id="T_45521_row0_col10"  >0</td>
      <td id="T_45521_row0_col11"  >0.34</td>
    </tr>
    <tr>
      <td id="T_45521_row1_col0"  >14-Dec-2021</td>
      <td id="T_45521_row1_col1"  >Group Gratuity Balanced Fund</td>
      <td id="T_45521_row1_col2"  >BALFND</td>
      <td id="T_45521_row1_col3"  >12</td>
      <td id="T_45521_row1_col4"  >50</td>
      <td id="T_45521_row1_col5"  >24.49</td>
      <td id="T_45521_row1_col6"  >25</td>
      <td id="T_45521_row1_col7"  >97.94</td>
      <td id="T_45521_row1_col8"  >85</td>
      <td id="T_45521_row1_col9"  >12</td>
      <td id="T_45521_row1_col10"  >3</td>
      <td id="T_45521_row1_col11"  >15.29</td>
    </tr>
    <tr>
      <td id="T_45521_row2_col0"  >14-Dec-2021</td>
      <td id="T_45521_row2_col1"  >Group Gratuity Growth Fund</td>
      <td id="T_45521_row2_col2"  >GRWFND</td>
      <td id="T_45521_row2_col3"  >13</td>
      <td id="T_45521_row2_col4"  >27</td>
      <td id="T_45521_row2_col5"  >48.32</td>
      <td id="T_45521_row2_col6"  >50</td>
      <td id="T_45521_row2_col7"  >96.64</td>
      <td id="T_45521_row2_col8"  >86</td>
      <td id="T_45521_row2_col9"  >11</td>
      <td id="T_45521_row2_col10"  >3</td>
      <td id="T_45521_row2_col11"  >13.96</td>
    </tr>
    <tr>
      <td id="T_45521_row3_col0"  >14-Dec-2021</td>
      <td id="T_45521_row3_col1"  >PNUV</td>
      <td id="T_45521_row3_col2"  >PNUV</td>
      <td id="T_45521_row3_col3"  >3</td>
      <td id="T_45521_row3_col4"  >9</td>
      <td id="T_45521_row3_col5"  >32.45</td>
      <td id="T_45521_row3_col6"  >0</td>
      <td id="T_45521_row3_col7"  >0.00</td>
      <td id="T_45521_row3_col8"  >100</td>
      <td id="T_45521_row3_col9"  >0</td>
      <td id="T_45521_row3_col10"  >0</td>
      <td id="T_45521_row3_col11"  >0.00</td>
    </tr>
    <tr>
      <td id="T_45521_row4_col0"  >14-Dec-2021</td>
      <td id="T_45521_row4_col1"  >Par Fund</td>
      <td id="T_45521_row4_col2"  >PPXX</td>
      <td id="T_45521_row4_col3"  >7595</td>
      <td id="T_45521_row4_col4"  >51493</td>
      <td id="T_45521_row4_col5"  >14.75</td>
      <td id="T_45521_row4_col6"  >15</td>
      <td id="T_45521_row4_col7"  >98.34</td>
      <td id="T_45521_row4_col8"  >78</td>
      <td id="T_45521_row4_col9"  >13</td>
      <td id="T_45521_row4_col10"  >4</td>
      <td id="T_45521_row4_col11"  >16.85</td>
    </tr>
    <tr>
      <td id="T_45521_row5_col0"  >14-Dec-2021</td>
      <td id="T_45521_row5_col1"  >Share Holders Fund</td>
      <td id="T_45521_row5_col2"  >SXXX</td>
      <td id="T_45521_row5_col3"  >27</td>
      <td id="T_45521_row5_col4"  >3258</td>
      <td id="T_45521_row5_col5"  >0.83</td>
      <td id="T_45521_row5_col6"  >0</td>
      <td id="T_45521_row5_col7"  >0.00</td>
      <td id="T_45521_row5_col8"  >0</td>
      <td id="T_45521_row5_col9"  >76</td>
      <td id="T_45521_row5_col10"  >24</td>
      <td id="T_45521_row5_col11"  >100.00</td>
    </tr>
    <tr>
      <td id="T_45521_row6_col0"  >14-Dec-2021</td>
      <td id="T_45521_row6_col1"  >Life Balanced Fund</td>
      <td id="T_45521_row6_col2"  >UEBAL</td>
      <td id="T_45521_row6_col3"  >1159</td>
      <td id="T_45521_row6_col4"  >3453</td>
      <td id="T_45521_row6_col5"  >33.55</td>
      <td id="T_45521_row6_col6"  >30</td>
      <td id="T_45521_row6_col7"  >111.84</td>
      <td id="T_45521_row6_col8"  >73</td>
      <td id="T_45521_row6_col9"  >18</td>
      <td id="T_45521_row6_col10"  >8</td>
      <td id="T_45521_row6_col11"  >26.61</td>
    </tr>
    <tr>
      <td id="T_45521_row7_col0"  >14-Dec-2021</td>
      <td id="T_45521_row7_col1"  >Life Conservative Fund</td>
      <td id="T_45521_row7_col2"  >UECON</td>
      <td id="T_45521_row7_col3"  >26</td>
      <td id="T_45521_row7_col4"  >260</td>
      <td id="T_45521_row7_col5"  >9.82</td>
      <td id="T_45521_row7_col6"  >10</td>
      <td id="T_45521_row7_col7"  >98.15</td>
      <td id="T_45521_row7_col8"  >85</td>
      <td id="T_45521_row7_col9"  >12</td>
      <td id="T_45521_row7_col10"  >3</td>
      <td id="T_45521_row7_col11"  >15.27</td>
    </tr>
    <tr>
      <td id="T_45521_row8_col0"  >14-Dec-2021</td>
      <td id="T_45521_row8_col1"  >Diversified Equity Fund</td>
      <td id="T_45521_row8_col2"  >UEDE1</td>
      <td id="T_45521_row8_col3"  >584</td>
      <td id="T_45521_row8_col4"  >602</td>
      <td id="T_45521_row8_col5"  >96.95</td>
      <td id="T_45521_row8_col6"  >100</td>
      <td id="T_45521_row8_col7"  >96.95</td>
      <td id="T_45521_row8_col8"  >71</td>
      <td id="T_45521_row8_col9"  >24</td>
      <td id="T_45521_row8_col10"  >5</td>
      <td id="T_45521_row8_col11"  >29.48</td>
    </tr>
    <tr>
      <td id="T_45521_row9_col0"  >14-Dec-2021</td>
      <td id="T_45521_row9_col1"  >Dynamic Opportunity Fund</td>
      <td id="T_45521_row9_col2"  >UEDO1</td>
      <td id="T_45521_row9_col3"  >309</td>
      <td id="T_45521_row9_col4"  >607</td>
      <td id="T_45521_row9_col5"  >50.87</td>
      <td id="T_45521_row9_col6"  >50</td>
      <td id="T_45521_row9_col7"  >101.74</td>
      <td id="T_45521_row9_col8"  >82</td>
      <td id="T_45521_row9_col9"  >13</td>
      <td id="T_45521_row9_col10"  >6</td>
      <td id="T_45521_row9_col11"  >18.11</td>
    </tr>
    <tr>
      <td id="T_45521_row10_col0"  >14-Dec-2021</td>
      <td id="T_45521_row10_col1"  >Amsure Dynamic Fund</td>
      <td id="T_45521_row10_col2"  >UEGFD</td>
      <td id="T_45521_row10_col3"  >1</td>
      <td id="T_45521_row10_col4"  >5</td>
      <td id="T_45521_row10_col5"  >14.37</td>
      <td id="T_45521_row10_col6"  >15</td>
      <td id="T_45521_row10_col7"  >95.81</td>
      <td id="T_45521_row10_col8"  >85</td>
      <td id="T_45521_row10_col9"  >11</td>
      <td id="T_45521_row10_col10"  >3</td>
      <td id="T_45521_row10_col11"  >14.53</td>
    </tr>
    <tr>
      <td id="T_45521_row11_col0"  >14-Dec-2021</td>
      <td id="T_45521_row11_col1"  >Amsure Income Fund</td>
      <td id="T_45521_row11_col2"  >UEGFI</td>
      <td id="T_45521_row11_col3"  >0</td>
      <td id="T_45521_row11_col4"  >1</td>
      <td id="T_45521_row11_col5"  >4.28</td>
      <td id="T_45521_row11_col6"  >5</td>
      <td id="T_45521_row11_col7"  >85.68</td>
      <td id="T_45521_row11_col8"  >88</td>
      <td id="T_45521_row11_col9"  >9</td>
      <td id="T_45521_row11_col10"  >3</td>
      <td id="T_45521_row11_col11"  >11.94</td>
    </tr>
    <tr>
      <td id="T_45521_row12_col0"  >14-Dec-2021</td>
      <td id="T_45521_row12_col1"  >Life Growth Fund</td>
      <td id="T_45521_row12_col2"  >UEGRW</td>
      <td id="T_45521_row12_col3"  >4480</td>
      <td id="T_45521_row12_col4"  >8708</td>
      <td id="T_45521_row12_col5"  >51.45</td>
      <td id="T_45521_row12_col6"  >50</td>
      <td id="T_45521_row12_col7"  >102.90</td>
      <td id="T_45521_row12_col8"  >86</td>
      <td id="T_45521_row12_col9"  >11</td>
      <td id="T_45521_row12_col10"  >3</td>
      <td id="T_45521_row12_col11"  >13.51</td>
    </tr>
    <tr>
      <td id="T_45521_row13_col0"  >14-Dec-2021</td>
      <td id="T_45521_row13_col1"  >High Growth Fund</td>
      <td id="T_45521_row13_col2"  >UEHGW</td>
      <td id="T_45521_row13_col3"  >1325</td>
      <td id="T_45521_row13_col4"  >1356</td>
      <td id="T_45521_row13_col5"  >97.74</td>
      <td id="T_45521_row13_col6"  >100</td>
      <td id="T_45521_row13_col7"  >97.74</td>
      <td id="T_45521_row13_col8"  >18</td>
      <td id="T_45521_row13_col9"  >66</td>
      <td id="T_45521_row13_col10"  >17</td>
      <td id="T_45521_row13_col11"  >82.26</td>
    </tr>
    <tr>
      <td id="T_45521_row14_col0"  >14-Dec-2021</td>
      <td id="T_45521_row14_col1"  >Growth Super</td>
      <td id="T_45521_row14_col2"  >UEPEQ</td>
      <td id="T_45521_row14_col3"  >9834</td>
      <td id="T_45521_row14_col4"  >10086</td>
      <td id="T_45521_row14_col5"  >97.50</td>
      <td id="T_45521_row14_col6"  >100</td>
      <td id="T_45521_row14_col7"  >97.50</td>
      <td id="T_45521_row14_col8"  >90</td>
      <td id="T_45521_row14_col9"  >8</td>
      <td id="T_45521_row14_col10"  >2</td>
      <td id="T_45521_row14_col11"  >10.37</td>
    </tr>
    <tr>
      <td id="T_45521_row15_col0"  >14-Dec-2021</td>
      <td id="T_45521_row15_col1"  >Pension Preserver Fund</td>
      <td id="T_45521_row15_col2"  >UEPWA</td>
      <td id="T_45521_row15_col3"  >46</td>
      <td id="T_45521_row15_col4"  >286</td>
      <td id="T_45521_row15_col5"  >16.03</td>
      <td id="T_45521_row15_col6"  >15</td>
      <td id="T_45521_row15_col7"  >106.86</td>
      <td id="T_45521_row15_col8"  >75</td>
      <td id="T_45521_row15_col9"  >18</td>
      <td id="T_45521_row15_col10"  >6</td>
      <td id="T_45521_row15_col11"  >24.58</td>
    </tr>
    <tr>
      <td id="T_45521_row16_col0"  >14-Dec-2021</td>
      <td id="T_45521_row16_col1"  >Pension Maximiser Fund</td>
      <td id="T_45521_row16_col2"  >UEPWC</td>
      <td id="T_45521_row16_col3"  >358</td>
      <td id="T_45521_row16_col4"  >744</td>
      <td id="T_45521_row16_col5"  >48.08</td>
      <td id="T_45521_row16_col6"  >50</td>
      <td id="T_45521_row16_col7"  >96.16</td>
      <td id="T_45521_row16_col8"  >74</td>
      <td id="T_45521_row16_col9"  >21</td>
      <td id="T_45521_row16_col10"  >5</td>
      <td id="T_45521_row16_col11"  >25.94</td>
    </tr>
    <tr>
      <td id="T_45521_row17_col0"  >14-Dec-2021</td>
      <td id="T_45521_row17_col1"  >Pension Balanced Fund</td>
      <td id="T_45521_row17_col2"  >UPBAL</td>
      <td id="T_45521_row17_col3"  >33</td>
      <td id="T_45521_row17_col4"  >89</td>
      <td id="T_45521_row17_col5"  >36.66</td>
      <td id="T_45521_row17_col6"  >30</td>
      <td id="T_45521_row17_col7"  >122.21</td>
      <td id="T_45521_row17_col8"  >72</td>
      <td id="T_45521_row17_col9"  >21</td>
      <td id="T_45521_row17_col10"  >7</td>
      <td id="T_45521_row17_col11"  >28.32</td>
    </tr>
    <tr>
      <td id="T_45521_row18_col0"  >14-Dec-2021</td>
      <td id="T_45521_row18_col1"  >Pension Conservative Fund</td>
      <td id="T_45521_row18_col2"  >UPCON</td>
      <td id="T_45521_row18_col3"  >1</td>
      <td id="T_45521_row18_col4"  >9</td>
      <td id="T_45521_row18_col5"  >9.66</td>
      <td id="T_45521_row18_col6"  >10</td>
      <td id="T_45521_row18_col7"  >96.59</td>
      <td id="T_45521_row18_col8"  >86</td>
      <td id="T_45521_row18_col9"  >11</td>
      <td id="T_45521_row18_col10"  >3</td>
      <td id="T_45521_row18_col11"  >14.42</td>
    </tr>
    <tr>
      <td id="T_45521_row19_col0"  >14-Dec-2021</td>
      <td id="T_45521_row19_col1"  >Pension Growth Fund</td>
      <td id="T_45521_row19_col2"  >UPGRW</td>
      <td id="T_45521_row19_col3"  >197</td>
      <td id="T_45521_row19_col4"  >370</td>
      <td id="T_45521_row19_col5"  >53.15</td>
      <td id="T_45521_row19_col6"  >50</td>
      <td id="T_45521_row19_col7"  >106.31</td>
      <td id="T_45521_row19_col8"  >82</td>
      <td id="T_45521_row19_col9"  >13</td>
      <td id="T_45521_row19_col10"  >5</td>
      <td id="T_45521_row19_col11"  >17.52</td>
    </tr>
    <tr>
      <td id="T_45521_row20_col0"  >14-Dec-2021</td>
      <td id="T_45521_row20_col1"  >Pension Growth Super</td>
      <td id="T_45521_row20_col2"  >UPPEQ</td>
      <td id="T_45521_row20_col3"  >299</td>
      <td id="T_45521_row20_col4"  >301</td>
      <td id="T_45521_row20_col5"  >99.13</td>
      <td id="T_45521_row20_col6"  >100</td>
      <td id="T_45521_row20_col7"  >99.13</td>
      <td id="T_45521_row20_col8"  >76</td>
      <td id="T_45521_row20_col9"  >20</td>
      <td id="T_45521_row20_col10"  >4</td>
      <td id="T_45521_row20_col11"  >24.25</td>
    </tr>
  </tbody>
</table>

This file when opened in browser displays correctly with all the required formatting.

However when i try to attach the html file to outlook emailbody, the formatting is all lost when mail is received

Below is the code which reads the html and attaches to outlook and sends email.

mail = outlook.CreateItem(0)
mail.To = toaddress
mail.Subject = mailsubject
mail.BodyFormat=3
mail.Display()

inspector = outlook.ActiveInspector()
word_editor = inspector.WordEditor

report_file = open(emailbodyfile,encoding='utf8')
html = report_file.read()

mail.HTMLBody =  html# doc.Content


mail.Send()
print('email sent')

the above html content is taken from the file referred to by the variable: emailbodyfile what is wrong here?

I have another code which generates html output (win32com used to save excel as html with formatting) which works fine

is it something to do with the html generated by dataframe.to_html()

Thanks in advance Regards, Kiran Jain

CodePudding user response:

First of all, make sure that you set the HTMLBody property to a well formed HTML string. Trying to assign a badly formatted HTML string may end up with a blank body.

Note, Outlook uses Word as an editor for message bodies. So, it applies its own rules the HTML markup. Read more about that in the Word HTML and CSS Rendering Capabilities in Outlook articles.

CodePudding user response:

resolved:

'border-spacing': '10px 50px'; 

above line in style section was causing the trouble :(

  • Related