使用 Python 和 win32com 仅使用“可以查看标题和位置”权限访问 Outlook 共享日历

问题描述 投票:0回答:1

我正在尝试使用 Python 和

win32com
客户端访问 Microsoft Outlook 中的共享日历, 但当日历的权限受到限制时,我遇到了问题。 我尝试访问的共享日历的权限设置为“可以查看标题和位置”。

我无权访问图形 API,并且不想跳过这些麻烦。

我只想阅读会议标题、地点和开始/结束时间。 因为我可以在 Outlook UI 中完美地看到这些,所以我认为必须有一种编程方式。

当我尝试迭代日历项目时,我收到以下(不是很有帮助)错误:

File "<file_path>", line <line_number>, in <module>
access_shared_calendar("<email_address>")
File "<file_path>", line <line_number>, in access_shared_calendar
for item in items:
File "<path_to_win32com>", line 322, in getitem
return self.get_good_object(self.enum.getitem(index))
File "<path_to_win32com>", line 41, in getitem
return self.__GetIndex(index)
File "<path_to_win32com>", line 62, in __GetIndex
result = self.oleobj.Next(1)
pywintypes.com_error: (-1075576561, 'OLE error 0xbfe4010f', None, None)

这是我用来访问共享日历的代码片段:

import win32com.client

def access_shared_calendar(mailbox):
    # Create an instance of Outlook
    outlook = win32com.client.Dispatch("Outlook.Application")
    
    # Get the Namespace object
    namespace = outlook.GetNamespace("MAPI")
    
    # Access the shared calendar
    recipient = namespace.CreateRecipient(mailbox)
    recipient.Resolve()
    if not recipient.Resolved:
        print(f"Could not resolve {mailbox}. Check the email address.")
        return
    
    # Open the shared calendar
    shared_calendar = namespace.GetSharedDefaultFolder(recipient, 9)  # 9 refers to the calendar folder
    items = shared_calendar.Items

    # Iterate over calendar items
    for item in items:
        print(f"Subject: {item.Subject}, Start: {item.Start}, End: {item.End}")

# Example usage
access_shared_calendar("[email protected]")

如有任何帮助,我们将不胜感激。谢谢!

python outlook com win32com outlook-2010
1个回答
0
投票

通过 win32com 处理 Outlook 中的受限权限时,访问共享日历可能并不简单。但是,您可以尝试使用 Restrict 方法来过滤要检索的项目来解决此问题。此方法允许您应用特定标准来限制返回的项目。

以下示例展示了如何修改代码以使用限制从共享日历中检索所需的属性(主题、位置、开始、结束):

import win32com.client

def access_shared_calendar(mailbox):
    # Create an instance of Outlook
    outlook = win32com.client.Dispatch("Outlook.Application")
    
    # Get the Namespace object
    namespace = outlook.GetNamespace("MAPI")
    
    # Access the shared calendar
    recipient = namespace.CreateRecipient(mailbox)
    recipient.Resolve()
    if not recipient.Resolved:
        print(f"Could not resolve {mailbox}. Check the email address.")
        return
    
    # Open the shared calendar
    shared_calendar = namespace.GetSharedDefaultFolder(recipient, 9)  # 9 refers to the calendar folder
    
    # Apply a filter to retrieve specific properties
    filter = "[Start] >= '01/01/2023' AND [End] <= '12/31/2023'"
    filtered_items = shared_calendar.Items.Restrict(filter)
    
    # Iterate over filtered calendar items
    for item in filtered_items:
        print(f"Subject: {item.Subject}, Location: {item.Location}, Start: {item.Start}, End: {item.End}")

# Example usage
access_shared_calendar("[email protected]")

调整过滤字符串以设置日历项目所需的日期范围或任何其他条件。此代码尝试根据日期范围(“01/01/2023”到“12/31/2023”)过滤项目并检索主题、位置、开始和结束时间等属性。

通过使用过滤并可能一次访问较少的属性,确保处理与受限权限相关的潜在错误。此外,您可能会考虑围绕访问特定属性进行错误处理,以避免因权限受限而遇到问题。

© www.soinside.com 2019 - 2024. All rights reserved.