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

如何用Python定时切换SharePoint在线仪表板?

解决方案

方案1:用Selenium控制浏览器(精准管理标签页,推荐)

这个方法能直接操控浏览器,提前打开所有需要的SharePoint仪表板标签页,之后定时在已有标签页间切换,完全不会重复开新标签。

步骤1:安装依赖

pip install selenium

另外要下载对应浏览器的驱动(比如Chrome的ChromeDriver,版本要和浏览器匹配),可以把驱动路径加到系统环境变量,或者在代码里直接指定路径。

步骤2:示例代码

from selenium import webdriver
import time

# 替换成你的SharePoint仪表板链接
dashboard_urls = [
    "https://your-sharepoint-site/dashboard1",
    "https://your-sharepoint-site/dashboard2",
    "https://your-sharepoint-site/dashboard3"
]

# 初始化Chrome浏览器(换成Firefox/Edge的话改webdriver.Firefox()/webdriver.Edge())
driver = webdriver.Chrome()

# 批量打开所有仪表板标签页
for url in dashboard_urls:
    driver.execute_script("window.open('');")
    driver.switch_to.window(driver.window_handles[-1])
    driver.get(url)
    time.sleep(5)  # 等待页面加载,根据实际情况调整时长

# 循环切换标签页展示
try:
    while True:
        for tab_handle in driver.window_handles:
            driver.switch_to.window(tab_handle)
            time.sleep(10)  # 每个仪表板展示10秒,按需修改
except KeyboardInterrupt:
    # 按Ctrl+C终止程序时关闭浏览器
    driver.quit()

额外优化:动态读取SharePoint表格的链接

如果要从SharePoint在线表格实时获取仪表板链接,不用手动改代码,可以用office365-rest-python-client库读取表格数据:

pip install office365-rest-python-client

示例读取代码:

from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.user_credential import UserCredential

# 你的SharePoint站点信息
site_url = "https://your-sharepoint-site"
username = "你的账号"
password = "你的密码"
list_name = "存储链接的SharePoint列表名称"

# 连接SharePoint并读取列表
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))
sp_list = ctx.web.lists.get_by_title(list_name)
items = sp_list.items.get().execute_query()

# 提取列表里的链接列数据(替换成你实际的列名)
dashboard_urls = [item.get("DashboardLink") for item in items]

方案2:用PyAutoGUI模拟键盘操作(适合简单场景)

如果不想用Selenium,也可以模拟浏览器的标签页切换快捷键,前提是你已经手动打开了所有需要的仪表板标签页,且浏览器处于前台。

步骤1:安装依赖

pip install pyautogui

步骤2:示例代码

import pyautogui
import time

display_time = 10  # 每个仪表板展示时长
tab_num = 3  # 标签页数量

try:
    while True:
        for _ in range(tab_num):
            pyautogui.hotkey('ctrl', 'tab')  # Windows/Chrome用这个,Mac换成'command', 'tab'
            time.sleep(display_time)
except KeyboardInterrupt:
    pass

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.06 22:05:11