I am working on python code which will add one string in excel file and will attach that file to email. I am sending this email using AWS SES.
When I am trying to run my code it is giving me below error -
TypeError: expected bytes-like object, not Workbook
Below is my code-
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()}
)
I know there is something wrong with workbook object. But I don't know how to resolve this issue. Can someone please help me out?