0

I am trying to embed some images into an email in Outlook using python. However, the images do not appear, but instead a box stating "The image cannot be displayed. Verify that the link points to the correct file and location." The link is correct as I have copy and pasted the line used to create the files.

Are my nested f-string incorrect or is there something else I am missing?

I've removed confidential info from the email, so looks a bit sparse, but have kept the lines with the image references.

def sendmail(region, service_line, gpn_list, cc_list):
inventory = exposure_final[(exposure_final['Engagement Region'] == region) &
                       (exposure_final['Engagement Service Line'] == service_line)]\
                  ['Exposure Inventory Balance'].sum()/1e6
inventory_string = "{:.1f}".format(inventory)
inventory_lastwk = last_wk[(last_wk['Engagement Region'] == region) &
                       (last_wk['Engagement Service Line'] == service_line)]\
                  ['Exposure Inventory Balance'].sum()/1e6
change = "{:.1f}".format((inventory - inventory_lastwk)*100/inventory_lastwk)
if float(change) > 0:
    direction = 'an increase of'
else:
    direction = 'a decrease of'
cleanedlist = [x for x in gpn_list if str(x) != 'nan']
emails = ';'.join(map(str, cleanedlist))
olMailItem = 0x0
obj = win32com.client.Dispatch("Outlook.Application")
newMail = obj.CreateItem(olMailItem)
newMail.Subject = f'{service_line} ({region}) inventory weekly as at FY21 P{period}w{week}'
 
html = f'''<p>Dear Partners,<br /><br />
AR and WIP as at FY21 P{period}w{week}: ${inventory_string}mil, {direction} {str(change)}% compared 
to last week.</p>
<p><img src={f'{directory}{folder_mth}{folder_wk}{region}_chart.jpg'} alt="" /><br />
<img src={f'{directory}{folder_mth}{folder_wk}{region}_table.jpg'} alt="" /><br /></p>
<p>Blah blah blah.<br /><img src={f'{directory}{folder_mth}{folder_wk}{region} 
{service_line}_partners.jpg'} alt="" /></p>
</p>'''

newMail.HTMLBody = html
newMail.To = emails
newMail.CC = cc_list
newMail.Display()
Omar
  • 27
  • 5
  • 1
    Nesting in f-string is unreadable and not beautiful. If you need complex templates your should check out one of the templating engines available for Python. Jinja2 might be a good start. – Klaus D. Jan 08 '21 at 07:15
  • Thanks - looking into it now. First I am hearing of templating engines tbh, but seems to be what I need. – Omar Jan 08 '21 at 07:25

0 Answers0