下面的代码发送包含文本和图像的电子邮件。我正在尝试将此电子邮件(文本和图像)转换为 pdf 文件并将文件下载到特定路径。有没有办法把它转换成pdf?
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.image import MIMEImage
strFrom = 'abc@outlook.com'
strTo = 'xyz@outlook.com'
msgRoot = MIMEMultipart('related')
msgRoot['Subject'] = 'Statistics'
msgRoot['From'] = strFrom
msgRoot['To'] = strTo
msgRoot.preamble = 'This is a multi-part message in MIME format.'
msgAlternative = MIMEMultipart('alternative')
msgRoot.attach(msgAlternative)
msgText = MIMEText('This is the alternative plain text message.')
msgAlternative.attach(msgText)
msgText = MIMEText('<b>Number of reports: </b>'+ str(df['COUNT'][0])
'<br><img src="cid:image1" table align="left">'
'<br><img src="cid:image2" table align="center"><br>','html')
msgAlternative.attach(msgText)
fp = open('plot1.png', 'rb')
msgImage1 = MIMEImage(fp.read())
fp.close()
msgImage1.add_header('Content-ID', '<image1>')
msgRoot.attach(msgImage1)
fp = open('plot.png', 'rb')
msgImage2 = MIMEImage(fp.read())
fp.close()
msgImage2.add_header('Content-ID', '<image2>')
msgRoot.attach(msgImage2)
import smtplib
smtpServer='***'
server = smtplib.SMTP(smtpServer)
server.sendmail(strFrom, strTo, msgRoot.as_string())
server.quit()