Python win32com Outlook HTMLbody 格式化目录错误

Python win32com Outlook HTMLbody formatting directory incorrectly

我正在尝试在 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\folder

我工作的那个人能够回答这个问题。

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 是什么。可能是个愚蠢的问题,但老实说我不知道​​。

回答你的第二个问题,"what does r in front of a string mean",意思是原始字符串。引用另一个 Whosebug 页面:

"The r means that the string is to be treated as a raw string, which means that all escape codes will be ignored"

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

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