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

如何用Python脚本从SharePoint下载多层嵌套文件夹及文件

批量下载SharePoint多层嵌套文件夹及文件解决方案

方法一:使用PnP PowerShell(推荐,操作简便)

PnP PowerShell是微软官方提供的SharePoint管理工具,原生支持递归操作,适合批量处理嵌套文件夹场景。

1. 安装PnP PowerShell

打开管理员权限的PowerShell,执行以下命令完成安装:

Install-Module -Name PnP.PowerShell -Force -AllowClobber

2. 连接到目标SharePoint站点

执行命令后,按弹出的窗口提示登录你的SharePoint账号:

Connect-PnPOnline -Url "https://abchome.sharepoint.com/sites/QualityGSharepoint" -Interactive

3. 执行递归下载脚本

复制以下脚本到PowerShell中,修改本地保存路径后运行:

function Download-SPFolder {
    param(
        [Parameter(Mandatory=$true)]
        [string]$ServerRelativeUrl,
        [Parameter(Mandatory=$true)]
        [string]$LocalPath
    )

    # 创建本地目标文件夹
    if (-not (Test-Path $LocalPath)) {
        New-Item -ItemType Directory -Path $LocalPath | Out-Null
    }

    # 获取当前文件夹下的所有文件和子文件夹
    $folderItems = Get-PnPListItem -List "Shared Documents" -FolderServerRelativeUrl $ServerRelativeUrl

    foreach ($item in $folderItems) {
        if ($item.FileSystemObjectType -eq "File") {
            # 下载文件到本地
            $fileSavePath = Join-Path $LocalPath $item.FieldValues.FileLeafRef
            Get-PnPFile -ServerRelativeUrl $item.FieldValues.FileRef -Path $fileSavePath -AsFile -Force
            Write-Host "已完成下载: $fileSavePath"
        }
        elseif ($item.FileSystemObjectType -eq "Folder") {
            # 递归处理子文件夹
            $subFolderUrl = $item.FieldValues.FileRef
            $subLocalPath = Join-Path $LocalPath $item.FieldValues.FileLeafRef
            Download-SPFolder -ServerRelativeUrl $subFolderUrl -LocalPath $subLocalPath
        }
    }
}

# 配置参数:起始SharePoint文件夹路径、本地保存路径
# 注意:本地路径请替换为你实际想要保存的文件夹
Download-SPFolder -ServerRelativeUrl "/sites/QualityGSharepoint/Shared Documents" -LocalPath "C:\SP_Downloads"

关键注意事项

  • 替换LocalPath为你的本地目标路径(如D:\SharePoint_Files)
  • 确保你的账号拥有目标站点Shared Documents的读取权限
  • 若遇到大文件下载超时,可在Get-PnPFile命令后添加-TimeoutSec 300(设置5分钟超时)

方法二:使用Python脚本(适合开发者)

如果习惯用Python,可借助office365-rest-python-client库实现递归下载:

1. 安装依赖库

pip install office365-rest-python-client

2. 递归下载脚本

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

def download_folder(ctx, folder_server_relative_url, local_save_path):
    # 创建本地文件夹
    if not os.path.exists(local_save_path):
        os.makedirs(local_save_path)
    
    # 获取当前文件夹对象
    folder = ctx.web.get_folder_by_server_relative_url(folder_server_relative_url)
    ctx.load(folder)
    ctx.execute_query()

    # 下载所有文件
    files = folder.files
    ctx.load(files)
    ctx.execute_query()
    for file in files:
        file_save_path = os.path.join(local_save_path, file.name)
        with open(file_save_path, "wb") as local_file:
            file.download(local_file).execute_query()
        print(f"已下载文件: {file_save_path}")

    # 递归处理子文件夹
    sub_folders = folder.folders
    ctx.load(sub_folders)
    ctx.execute_query()
    for sub_folder in sub_folders:
        sub_local_path = os.path.join(local_save_path, sub_folder.name)
        download_folder(ctx, sub_folder.serverRelativeUrl, sub_local_path)

# 配置参数
site_url = "https://abchome.sharepoint.com/sites/QualityGSharepoint"
username = "你的SharePoint账号"
password = "你的账号密码"
target_folder_url = "/sites/QualityGSharepoint/Shared Documents"
local_root_path = "C:\SP_Downloads"

# 初始化连接
ctx = ClientContext(site_url).with_credentials(UserCredential(username, password))

# 启动下载
download_folder(ctx, target_folder_url, local_root_path)

关键注意事项

  • 替换username、password、local_root_path为你的实际信息
  • 若账号启用了MFA,需改用ClientCredential或其他认证方式,可参考库文档调整

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.05 21:45:29