要在 Python 中下载 SharePoint 文件,你可以使用'requests'库来发送 HTTP 请求并下载文件。以下是一个简单的示例代码:

import requests

# SharePoint 网站的 URL
sharepoint_url = 'https://example.sharepoint.com/sites/your_site'

# 文件在 SharePoint 中的路径
file_path = '/sites/your_site/Shared Documents/example.docx'

# SharePoint 凭据
username = 'your_username'
password = 'your_password'

# 设置请求头
headers = {
    'Accept': 'application/json;odata=verbose'
}

# 构建请求的 URL
url = f'{sharepoint_url}/_api/web/GetFileByServerRelativeUrl('{file_path}')/$value'

# 发送请求并下载文件
response = requests.get(url, headers=headers, auth=(username, password))
if response.status_code == 200:
    # 提取文件名
    file_name = file_path.split('/')[-1]
    
    # 保存文件
    with open(file_name, 'wb') as f:
        f.write(response.content)
    print(f'文件已下载到:{file_name}')
else:
    print('下载文件时出错')

请注意,上述示例代码中,你需要替换'sharepoint_url'、'file_path'、'username' 和 'password' 为你自己的实际值。此外,'requests' 库需要安装,你可以使用'pip install requests'命令进行安装。


原文地址: https://www.cveoy.top/t/topic/o6Yc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录