900字范文,内容丰富有趣,生活中的好帮手!
900字范文 > python邮件正文表格怎么编辑_如何使用python将excel文件的内容复制粘贴到电子邮件正文中...

python邮件正文表格怎么编辑_如何使用python将excel文件的内容复制粘贴到电子邮件正文中...

时间:2021-09-07 18:12:48

相关推荐

python邮件正文表格怎么编辑_如何使用python将excel文件的内容复制粘贴到电子邮件正文中...

我正在写一个python脚本来发送电子邮件。现在它正在以附件的形式发送一个excel文件,但是我想发送包含excel文件内容的邮件。比如用颜色、格式等复制excel文件的内容,然后粘贴到邮件正文中。我该怎么做?在

我的python脚本如下:#!/usr/local/bin/python2.7

import smtplib,email,email.encoders,email.mime.text,email.mime.base

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

from email import encoders

from email.message import Message

from email.mime.audio import MIMEAudio

from email.mime.base import MIMEBase

from email.mime.image import MIMEImage

from email.mime.multipart import MIMEMultipart

from email.mime.text import MIMEText

import schedule

import time

msg = MIMEMultipart()

# me == my email address

# you == recipient's email address

me = "abc@"

you = "abc@"

# Create message container - the correct MIME type is multipart/alternative.

msg = MIMEMultipart('mixed')

msg['Subject'] = "Automation Testing"

msg['From'] = me

msg['To'] = you

# Create the body of the message (a plain-text and an HTML version).

text = "Hi\n\nPlease find attached the weekly report."

part1 = MIMEText(text, 'plain')

#attach an excel file:

fp = open('/Users/excel.xlsm', 'rb')

file1=email.mime.base.MIMEBase('application','vnd.ms-excel')

file1.set_payload(fp.read())

fp.close()

email.encoders.encode_base64(file1)

file1.add_header('Content-Disposition','attachment;filename=excelsheet.xlsx')

# Attach parts into message container.

# According to RFC 2046, the last part of a multipart message, in this case

# the HTML message, is best and preferred.

msg.attach(part1)

msg.attach(file1)

composed = msg.as_string()

fp = open('msgtest.txt', 'w')

fp.write(composed)

# Credentials (if needed)

# The actual mail send

server = smtplib.SMTP('', 259)

server.starttls()

server.sendmail(me, you, composed)

server.quit()

fp.close()

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。