我正在研究将在excel文件中添加一个字符串并将该文件附加到电子邮件的python代码。我正在使用AWS SES发送此电子邮件。
当我尝试运行我的代码时,它给我以下错误-
TypeError: expected bytes-like object,not Workbook
下面是我的代码-
import boto3
import xlsxwriter
from email.mime.text import MIMEText
from email.mime.application import MIMEApplication
from email.mime.multipart import MIMEMultipart
def lambda_handler(event,context):
client = boto3.client('ses',region_name=AWS_REGION)
sender = "xxx@gmail.com"
to = "aaa@gmail.com"
workbook = xlsxwriter.Workbook('abc.xlsx')
worksheet = workbook.add_worksheet()
worksheet.write('A1','Hello..')
#send email with attachment
msg = MIMEMultipart()
msg['Subject'] = 'Test Email'
msg['From'] = sender
msg['To'] = to
body_text = MIMEText(BODY_TEXT,"html")
attachment = MIMEApplication(workbook)
attachment.add_header('Content-Disposition','attachment',filename='Expenses01.xlsx')
msg.attach(attachment)
msg.attach(body_text)
response = client.send_raw_email(
Source=sender,Destinations=[to],RawMessage={"Data": msg.as_string()}
)
我知道工作簿对象有问题。但是我不知道如何解决这个问题。有人可以帮我吗?