You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在MacOS系统下使用Python访问Outlook日历?

MacOS下用Python访问Outlook日历的可行方案

方法一:Microsoft Graph API(官方跨平台方案)

这是微软官方提供的接口,跨Mac、Windows、Linux平台,功能完整,能获取所有日历相关数据。

步骤如下:

  1. 去Azure门户注册一个应用,申请Calendars.Read权限(允许读取日历)
  2. 用Python的msal库完成身份认证,获取访问令牌
  3. 调用Graph API的日历接口,提取事件标题

极简示例代码:

import msal
import requests

# 替换成你自己的Azure应用信息
CLIENT_ID = "你的客户端ID"
TENANT_ID = "common"
SCOPE = ["https://graph.microsoft.com/Calendars.Read"]

# 初始化MSAL客户端
app = msal.PublicClientApplication(CLIENT_ID, authority=f"https://login.microsoftonline.com/{TENANT_ID}")

# 获取令牌(第一次会弹出浏览器登录)
result = app.acquire_token_interactive(SCOPE)

if "access_token" in result:
    # 调用Graph API获取日历事件
    response = requests.get(
        "https://graph.microsoft.com/v1.0/me/events",
        headers={"Authorization": f"Bearer {result['access_token']}"}
    )
    events = response.json().get("value", [])
    # 打印所有事件标题
    for event in events:
        print(event.get("subject"))
else:
    print("认证失败:", result.get("error"))

注意:先安装依赖pip install msal requests,注册Azure应用的步骤可参考微软官方文档,流程清晰,初学者也能完成。

方法二:AppleScript + Python(本地直接访问)

Mac版Outlook支持AppleScript,Python可通过调用系统osascript命令,直接读取本地Outlook数据,无需联网,步骤更简单。

示例代码:

import subprocess

# 编写AppleScript脚本,获取Outlook日历事件标题
script = '''
tell application "Microsoft Outlook"
    set all_events to every calendar event
    set event_titles to {}
    repeat with an_event in all_events
        set end of event_titles to subject of an_event
    end repeat
    return event_titles
end tell
'''

# 调用osascript执行脚本
result = subprocess.run(
    ["osascript", "-e", script],
    capture_output=True,
    text=True
)

if result.returncode == 0:
    # 处理返回的标题列表
    event_titles = result.stdout.strip().split(", ")
    for title in event_titles:
        print(title)
else:
    print("执行失败:", result.stderr)

注意:第一次运行时,Mac会弹出权限请求,需允许你的Python环境(比如终端、VS Code)访问Microsoft Outlook,否则会报错。

给初学者的建议

  • 若仅本地使用,不想配置云服务,优先选AppleScript方法,上手快,无复杂流程
  • 若需要跨平台或后续扩展功能(比如修改日历、同步数据),选Graph API,这是长期稳定的官方方案

内容的提问来源于stack exchange,提问作者Jess

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 20:10:32