【问题标题】:Python Outlook - Add date to emailPython Outlook - 将日期添加到电子邮件
【发布时间】:2021-07-16 11:31:21
【问题描述】:

我已使用 Python 脚本自动发送 Outlook 电子邮件。我现在想做的是在电子邮件正文中输入下周日期。

有什么功能可以让我这样做吗?

例如,我想发送一封电子邮件,并且我希望在电子邮件中要求收件人在 4 月 29 日之前回复(从今天开始正好一周)。有没有办法可以读取今天的日期,然后在电子邮件中打印出 7 天后的日期?

示例代码: 导入 win32com.client 作为客户端 导入路径库 将熊猫导入为 pd

outlook = client.Dispatch('Outlook.Application')

#Mail item
message = outlook.CreateItem(0)


df = pd.read_excel(r'Desktop\Review.xlsx',index_col=False,sheet_name='Review',  usecols = "A:H")

#Display message
body = df.to_html()

message.Display()

message.To = "Mick.callnan@something.com"
message.Subject = "Review"
message.HTMLBody = "Hi All, <br> <br>Please respond by this day next week **Enter date here**
#message.Send()

【问题讨论】:

    标签: python html pywin32 win32com


    【解决方案1】:
    import datetime
    
    # how many days allowance?
    N = 7
    
    # assign the deadline date
    deadline = datetime.date.today() + datetime.timedelta(days=N)
    
    # on its own, it already works...
    print(f"Please respond by {deadline}.")
    
    # prints out Please respond by 2021-04-29.
    
    # but perhaps you want to format it 
    s = deadline.strftime("%d %b %Y")
    
    print(f"Please respond by {s}.")
    
    # prints out Please respond by 29 Apr 2021.
    

    对了,格式码请自行查看https://docs.python.org/3/library/datetime.html#strftime-strptime-behavior

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-11-15
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多