我已經成功創建了一個發送多封電子郵件的程式,但我沒有成功正確地格式化文本。
每次我嘗試創建一個新行時,python 都會自動插入一個新段落,我不希望這樣。我試圖將 .lstrip 放在文本塊的 ent 之后,在 """ 之后,但沒有用。
文本應該是這樣的:
"Dear owner,
Our records indicate that your payment have not been received so far.
The total due is:"
但是正在像這樣列印在電子郵件上:
"Dear owner
Our records indicate that your payment have not been received so far.
The total due is:"
請在下面查看我的代碼,感謝您的任何輸入。
import email
import win32com.client as client
import pandas as pd
tabela = pd.read_excel('over60.xlsx')
#print(tabela)
data = tabela[['Customer','Email','Owner','Total Due']].values.tolist()
for dado in data:
owner_number = dado[0]
to = dado[1]
owner = dado[2]
amount = dado[3]
outlook = client.Dispatch('Outlook.Application')
account = outlook.session.Accounts['myemail.com']
email = outlook.CreateItem(0)
email.To = 'myemail.com'
email.Subject = f'Past Due Notice - {owner} - {owner_number}'
email.Body =f""" Dear {owner},
Our records indicate that your payment have not been received so far.
The total due is ${amount: .2f}.
uj5u.com熱心網友回復:
多行字串包括行首的縮進,嘗試取消行首一直到螢屏左側的縮進,或者用\n
例如替換換行符:
f"""Dear {owner},\n\nOur records indicate that your payment have not been received so far.\n\nThe total due is ${amount: .2f}.
\n
呈現為換行符而不是文字字符“\n”
uj5u.com熱心網友回復:
使用"""
保留空格,所以這樣的字串......
"""Dear {owner},
Our records indicate that your payment have not been received so far.
The total due is ${amount: .2f}."""
實際上把那些領先的空格放進去。
如果你想繼續使用多行字串,你可以應用類似inspect.cleandoc
清理字串的東西,像這樣......
import inspect
# ... your code ...
body = f""" Dear {owner},
Our records indicate that your payment have not been received so far.
The total due is ${amount: .2f}."""
email.Body = inspect.cleandoc(body)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/496249.html