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

如何使用Python获取并打印Windows 10系统显示的通知内容

Python 监听Windows 10通知实现方案

前置准备

  • 首先安装依赖库:
    执行命令 pip install winrt-sdk
  • 开通通知访问权限:进入Windows设置 → 系统 → 通知 → 下拉找到“通知访问权限”,给对应Python执行程序开启权限
  • 确保WhatsApp桌面端的系统通知权限已开启

实现代码示例

import asyncio
from winrt.windows.ui.notifications.management import UserNotificationListener
from winrt.windows.ui.notifications import KnownNotificationBindings

async def listen_notifications():
    # 获取通知监听器实例
    listener = UserNotificationListener.current
    # 检查权限
    access_status = await listener.request_access_async()
    if access_status != 1: # 1代表权限已授予
        print("未获取通知访问权限,请在系统设置中开启")
        return

    def on_notification_changed(sender, args):
        notification = sender.get_notification(args.user_notification_id)
        # 过滤WhatsApp发送的通知
        if notification.app_info.display_name == "WhatsApp":
            # 提取通知文本内容
            toast_notification = notification.notification
            binding = toast_notification.visual.get_binding(KnownNotificationBindings.toast_text_generic)
            if binding:
                text_elements = binding.get_text_elements()
                msg_content = "\n".join([str(t.text) for t in text_elements])
                print("收到WhatsApp通知内容:")
                print(msg_content)
                # 此处可追加正则匹配逻辑,提取Zoom会议ID和密码

    # 绑定通知变更事件
    listener.add_notification_changed(on_notification_changed)
    print("已开始监听通知,按Ctrl+C停止")
    # 保持程序运行
    while True:
        await asyncio.sleep(1)

if __name__ == "__main__":
    asyncio.run(listen_notifications())

后续扩展建议

  • 提取到WhatsApp消息内容后,可以用正则匹配Zoom会议规则:
    会议ID一般是\d{3}-\d{4}-\d{3}或者连续10/11位数字,会议密码一般会跟在Passcode:、密码:这类关键词后面,写对应正则规则即可提取
  • 提取到ID和密码后,可以调用Zoom的命令行启动参数自动加入会议,示例命令格式:
    zoom.exe --url=zoommtg://zoom.us/join?confno=会议ID&pwd=会议密码

内容的提问来源于stack exchange,提问作者NITISH KUMAR VIIIE 12

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.10 20:42:01