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

如何刷新Excel查询并导入Pandas DataFrame?无GraphAPI的SharePoint访问方案

机器账户访问SharePoint文件并转为Pandas DataFrame

方案1:服务主体认证访问现代SharePoint站点

用office365-rest-python-client库,依托Azure AD服务主体(机器账户)完成认证,直接获取SharePoint文件内容并转为DataFrame。

步骤与代码

  1. 安装依赖:
pip install office365-rest-python-client pandas openpyxl
  1. 代码实现:
from office365.sharepoint.client_context import ClientContext
from office365.runtime.auth.client_credential import ClientCredential
import pandas as pd
from io import BytesIO

# 替换为你的配置信息
tenant_id = "你的租户ID"
client_id = "服务主体客户端ID"
client_secret = "服务主体客户端密钥"
site_url = "https://你的租户.sharepoint.com/sites/目标站点"
file_server_relative_path = "/sites/目标站点/Shared Documents/目标文件.xlsx"

# 初始化认证上下文
ctx = ClientContext(site_url).with_credentials(ClientCredential(client_id, client_secret))
# 获取文件内容
file = ctx.web.get_file_by_server_relative_url(file_server_relative_path)
file_content = file.read().execute_query()

# 读取为Pandas DataFrame
df = pd.read_excel(BytesIO(file_content))
print(df.head())

注意事项

  • 服务主体需提前在Azure AD中创建,并授予目标SharePoint站点的访问权限(如站点成员/拥有者)。
  • 适用于现代SharePoint Online站点。

方案2:NTLM认证访问经典SharePoint站点

针对传统本地/经典SharePoint站点,使用NTLM认证结合requests库下载文件。

步骤与代码

  1. 安装依赖:
pip install requests requests-ntlm pandas openpyxl
  1. 代码实现:
import requests
from requests_ntlm import HttpNtlmAuth
import pandas as pd
from io import BytesIO

# 替换为你的机器账户凭据和文件URL
machine_username = "DOMAIN\\机器账户名"
machine_password = "机器账户密码"
file_url = "http://经典SharePoint站点/sites/目标站点/Shared Documents/目标文件.xlsx"

# 发起请求下载文件
response = requests.get(file_url, auth=HttpNtlmAuth(machine_username, machine_password))
response.raise_for_status()  # 捕获请求错误

# 转为DataFrame
df = pd.read_excel(BytesIO(response.content))
print(df.head())

注意事项

  • 需确保机器账户拥有目标文件的读取权限。
  • 仅适用于支持NTLM认证的经典SharePoint环境。

刷新Excel中的SharePoint查询并导入DataFrame

通过pywin32库操控Excel应用,完成查询刷新后读取数据至DataFrame,仅支持Windows环境。

步骤与代码

  1. 安装依赖:
pip install pywin32 pandas openpyxl
  1. 代码实现:
import win32com.client as win32
import pandas as pd
import os

# 替换为你的Excel文件路径和目标工作表名
excel_file_path = r"C:\本地路径\包含查询的文件.xlsx"
target_sheet = "数据工作表"

# 启动后台Excel进程
excel_app = win32.gencache.EnsureDispatch('Excel.Application')
excel_app.Visible = False  # 隐藏Excel窗口

try:
    # 打开工作簿
    workbook = excel_app.Workbooks.Open(excel_file_path)
    
    # 刷新所有查询并等待完成
    workbook.RefreshAll()
    excel_app.CalculateUntilAsyncQueriesDone()
    
    # 可选:保存刷新后的文件
    workbook.Save()
    
    # 读取指定工作表为DataFrame
    df = pd.read_excel(excel_file_path, sheet_name=target_sheet)

finally:
    # 清理资源,避免残留Excel进程
    workbook.Close()
    excel_app.Quit()
    del workbook, excel_app

print(df.head())

注意事项

  • Excel文件中的SharePoint查询需已配置正确的数据源,且机器账户拥有该数据源的访问权限。
  • 运行代码的Windows环境需安装Microsoft Excel。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.21 08:57:40