![](/img/trans.png)
[英]ImportError: No module named win32com.client (I'm sure I have pywin32 installed)
[英]Get Outlook Group Calendars using Pywin32 (win32com.client)
我試圖弄清楚如何訪問我所屬的不同 Outlook 組的日歷。 我正在使用 win32com 和 Python 3.9 來完成這項任務,並且希望避免使用 RESTful/auth 令牌路線,因為這對於應該是插入一些日歷約會的簡單腳本來說是相當大的開銷。
我可以使用以下代碼進行自己的約會:
import win32com.client
application = win32com.client.Dispatch('Outlook.Application')
namespace = application.GetNamespace('MAPI')
cal = namespace.GetDefaultFolder(9)
for item in cal.Items:
print(item.Subject)
返回我個人日歷中每個約會的主題行。
我還可以使用 GetSharedDefaultFolder 獲得相同的信息:
application = win32com.client.Dispatch('Outlook.Application')
namespace = application.GetNamespace('MAPI')
recipient = namespace.createRecipient("{my_email}")
resolved = recipient.Resolve()
sharedCalendar = namespace.GetSharedDefaultFolder(recipient, 9)
for item in sharedCalendar.Items:
print(item.Subject)
我讀到您想將創建所需日歷的人的電子郵件/用戶作為收件人傳遞,但我沒有運氣。
在嘗試使用創建者的 email 時,我收到以下錯誤:
pywintypes.com_error: (-2147352567, 'Exception occurred.', (4096, 'Microsoft Outlook', 'The operation failed because of a registry or installation problem. Restart Outlook and try again. If the problem persists, reinstall.', None, 0, -2147221219), None)
如果我嘗試使用他們的 (lastName, firstName),我會得到以下信息:
pywintypes.com_error: (-2009857777, 'OLE error 0x8834010f', None, None)
請注意,我指的是GROUP日歷,而不是共享日歷。 我不確定兩者之間是否真的有區別,但對我來說,它們在 Outlook 上顯示為不同的部分。
我一直在參考的一些參考資料(對於找到此頁面並遇到此問題的其他人):
通過 Python 讀取 Outlook 事件
https://docs.microsoft.com/en-us/office/vba/api/outlook.namespace.getdefaultfolder (和相關頁面)
通過 python 讀取 Outlook 共享日歷時出現問題
https://docs.microsoft.com/en-us/answers/questions/607061/how-to-create-new-events-share-calendar-with-pytho.html
NameSpace.GetSharedDefaultFolder方法用於委派方案,其中一個用戶已將一個或多個默認文件夾(例如,他們的共享日歷文件夾)的訪問權限委派給另一個用戶。 因此,您需要確保用戶作為授權訪問您。
如果日歷在 Outlook 的導航窗格中可見,則可以使用NavigationGroups object 訪問它。 您可以使用NavigationGroups
和NavigationFolders
collections 在導航窗格中遍歷模塊的組和文件夾層次結構。 NavigationModule
object 的NavigationGroups
集合包含導航模塊中顯示的每個導航組,而NavigationGroup
object 的NavigationFolders
集合包含導航組中顯示的每個導航文件夾。 通過組合使用這些 collections,您可以枚舉導航窗格中顯示的導航模塊的每個導航文件夾。 這是示例 VBA 代碼,您可以在其中了解所需的 OOM 屬性方法以獲取所需的日歷:
Dim WithEvents objPane As NavigationPane
Private Sub EnumerateActiveCalendarFolders()
Dim objModule As CalendarModule
Dim objGroup As NavigationGroup
Dim objFolder As NavigationFolder
Dim intCounter As Integer
On Error GoTo ErrRoutine
' Get the NavigationPane object for the
' currently displayed Explorer object.
Set objPane = Application.ActiveExplorer.NavigationPane
' Get the CalendarModule object, if one exists,
' for the current Navigation Pane.
Set objModule = objPane.Modules.GetNavigationModule(olModuleCalendar)
' Iterate through each NavigationGroup contained
' by the CalendarModule.
For Each objGroup In objModule.NavigationGroups
' Iterate through each NavigationFolder contained
' by the NavigationGroup.
For Each objFolder In objGroup.NavigationFolders
' Check if the folder is selected.
If objFolder.IsSelected Then
intCounter = intCounter + 1
End If
Next
Next
' Display the results.
MsgBox "There are " & intCounter & " selected calendars in the Calendar module."
EndRoutine:
On Error GoTo 0
Set objFolder = Nothing
Set objGroup = Nothing
Set objModule = Nothing
Set objPane = Nothing
intCounter = 0
Exit Sub
ErrRoutine:
MsgBox Err.Number & " - " & Err.Description, _
vbOKOnly Or vbCritical, _
"EnumerateActiveCalendarFolders"
End Sub
Outlook object model 對於所有類型的編程語言都是通用的,因此無需花費大量精力來識別完成工作所需的屬性和方法的順序。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.