Python win32com Outlook HTMLbody格式化目录不正确

时间:2015-08-24 23:49:18

标签: python outlook

我正在尝试使用Outlook电子邮件正文中的win32com向共享目录发送超链接。当程序运行时,我不确定我的路径发生了什么,使目录显示在下面。

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'EMAIL ADDRESSES'
mail.Subject = 'Subject'
mail.HTMLbody = ("Hello All -<br><br>"
         "Please find the following files in the shared drive:<br>"
         "<a href='\\servername1\apps\folder'>"
         "\\servername1\apps\folder</a><br><br>"
         "The file names are:<br>"
         "FILENAMES")
mail.send

文件路径在电子邮件中显示为: \ servername1pps \文件夹

2 个答案:

答案 0 :(得分:0)

我工作的家伙能够回答这个问题。

import win32com.client as win32
outlook = win32.Dispatch('outlook.application')
mail = outlook.CreateItem(0)
mail.To = 'EMAIL ADDRESSES'
mail.Subject = 'Subject'
mail.HTMLbody = (r"""Hello All -<br><br>
     Please find the following files in the shared drive:<br>
     <a href='\\servername1\apps\folder'>
     \\servername1\apps\folder</a><br><br>
     The file names are:<br>
     FILENAMES""")
mail.send

我们添加了“r”后跟三重引号并且有效。

不确定r是什么意思,但它有效。也许有人可以向我解释r是什么。可能是一个愚蠢的问题,但老实说我不知道​​。

答案 1 :(得分:0)

要回答你的第二个问题,“字符串前面的r是什么意思”,它意味着一个原始字符串。引用另一个stackoverflow页面:

“r表示该字符串将被视为原始字符串,这意味着将忽略所有转义码”

因此,如果你有一个文件路径,其中文件夹名称之间有空格,你必须使用r前缀。例如:r“C:\ Users \ A010101 \ Desktop \ Space between \ file.txt”

请参阅有关r前缀的问题页面: What does preceding a string literal with "r" mean?