邮箱正文发送 HTML 和 EXCEL 样式【必读】

1.excel 转 htlm

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application’)
wb = excel.Workbooks.Open(r’C:\Users\Administrator\Desktop\ 年金与养老金产品每日快报 20210608.xlsx’)
excel.Visible = True
ws = wb.Worksheets(‘Sheet1’)
ob = wb.PublishObjects.Add(1,r’C:\Users\Administrator\Desktop\ 年金与养老金产品每日快报 20210608.html’,‘Sheet1’)
ob.Publish(True)

2.pandas 写多个 sheet

writer=pd.ExcelWriter(self.new_excel)
end_df.to_excel(writer,sheet_name=sheet_name,index=0)
writer.save()

3. xls 转 xlsx

import win32com.client as win32

excel = win32.gencache.EnsureDispatch(‘Excel.Application’)
wb = excel.Workbooks.Open(excel_path)
wb.SaveAs(new_path, FileFormat = 51) #FileFormat = 51 is for .xlsx extension
wb.SaveAs(fname[:-1], FileFormat = 56) #FileFormat = 56 is for .xls extension
wb.Close()
excel.Application.Quit()

import pandas as pd
df = pd.read_excel(excel_path)
df.to_excel(new_excel,index=False)

4. 邮件发送表格形式格式正文

1.excel 转 html
2. 读取 html
f = open(self.new_html, ‘r’)
mail_body = f.read()
f.close()
3.mail_body 放到正文

邮箱正文发送 HTML 和 EXCEL 样式【必读】