简体   繁体   中英

F-String in mail python

when I use f-string to print supplier names, I use this code:

for i in range(1,10):
    print(f'{df_supplier_number["SUPPLIER NAME"][i]}')

The result looks like this:

supplier 1
supplier 2
supplier 3
supplier 4
supplier 5
...

But when using this method to write an email, I does not work.

for i in range(1,10):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.Subject = f'{df_supplier_number["SUPPLIER NAME"][i]}' + "_test"
    mail.To = "test@mail.com"
    mail.HTMLBody = r"""
    Hallo,<br><br>
    test """f"{df_supplier_number[i]}"r""" erstellen:<br><br>
    Mit dem Text:<br>
    test1 by """f"{df_supplier_number[i]}"r""".<br>
    Settlement Date Range: 01. November 2020 to 31. October 2021.<br><br>
    Und dem Betrag:<br>
    """f'{round(df_supplier_number[i],2)}'r""" € (wie in der angehängten Excel-Datei zu erkennen).<br><br>
    test
    """
    mail.Attachments.Add(os.getcwd()+ "\\" + f'{df_number[i]}'"_test.xlsx")
  
    mail.Send()

Thanks in advance!

It looks like you may have gone wild with r-string and f-string. I placed a single pair at the beginning of the HTMLBODY portion and the removed addition r,f, & potentially unneeded ". Please try the below out and see if it works for you. I made a random list of numbers to test out and everything seems to be working on my end.

for i in range(1,10):
    outlook = win32.Dispatch('outlook.application')
    mail = outlook.CreateItem(0)
    mail.Subject = f'{df_supplier_number["SUPPLIER NAME"][i]}' + "_test"
    mail.To = "test@mail.com"
    mail.HTMLBody = rf"""
        Hallo,<br><br>
        test "{df_supplier_number[i]}" erstellen:<br><br>
        Mit dem Text:<br>
        test1 by "{df_supplier_number[i]}".<br>
        Settlement Date Range: 01. November 2020 to 31. October 2021.<br><br>
        Und dem Betrag:<br>
        '{round(df_supplier_number[i],2)}'€ (wie in der angehängten Excel-Datei     
zu erkennen).<br><br>
test
"""
mail.Attachments.Add(os.getcwd()+ "\\" + f'{df_number[i]}'"_test.xlsx")

mail.Send()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM