用 Python 下载 Sharepoint Excel 文件 [英] Download Sharepoint Excel File in Python

查看:40
本文介绍了用 Python 下载 Sharepoint Excel 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Python 脚本从 SharePoint 存储库下载 Excel 文件.我正在使用 https:/的示例中定义的 Office365-Rest-Python-Client/github.com/vgrem/Office365-REST-Python-Client 并且我可以访问我需要的所有文件/目录.当我想下载任何文件时,问题就出现了.我尝试了几种方法,但都没有奏效:wget.download("https://shprepos.com/path/file.xlsx", local_path, bar=None)

I'm trying to download an Excel file from a SharePoint repository using a Python script. I'm using the Office365-Rest-Python-Client as defined in the examples at https://github.com/vgrem/Office365-REST-Python-Client and I have access to all the files/directories I need. The problem comes when I want to download any of the files. I've tried several approaches, but none of them works: wget.download("https://shprepos.com/path/file.xlsx", local_path, bar=None)

但我收到403 FORBIDDEN"错误.我也尝试过请求:

But I get a "403 FORBIDDEN" error. And I also tried with requests:

req = requests.get(ruta, auth=requests.auth.HTTPBasicAuth(username, password), headers=headers)
with open(local_file, 'wb') as file:
    file.write(req.content)

通过这段代码,我得到的是网页,而不是 excel 文件,我不明白为什么,因为如果我访问 url "https://shprepos.com/path/file.xlsx",使用正确的身份验证下载文件.

And with this code I'm getting the webpage, not the excel file, and I don't understan why, because if I access the url "https://shprepos.com/path/file.xlsx", with the correct authentication I download the file.

您知道使用 wget 使用身份验证下载该文件的方法吗?还是我在 requests.get 中做错了什么?

Do you know a way of downloading that file with wget using the authentication? Or am I doing something wrong in the requests.get ?

我需要一种获取该文件的方法,使用我在脚本开始时所做的先前身份验证:

I need a way of getting that file, using the previous authentication I did at the begining of the script:

ctx_auth = AuthenticationContext(shp_url)
token = ctx_auth.acquire_token_for_user(username, password)

你知道这样做的方法吗?可能python客户端有下载文件的方法但是我找不到!

Do you know a way of doing this? Maybe the python Client has a method for downloading the files but I can't find it!

非常感谢!:)

问候

推荐答案

是的!我找到了解决方案!!我需要获得授权才能下载文件.我在 Office365-Python-Client 的 test 文件夹中找到了一个示例.所以基本上,在通过请求获取 url 之前,您会获得授权:

Yep! I found the solution!! I needed to get the authorization before I can download the file. I found an example within the test folder of the Office365-Python-Client. So basically, before getting the url with the request, you get the authorization:

options = RequestOptions(shp_file_path)
ctx_auth.authenticate_request(options)
options.headers["X-FORMS_BASED_AUTH_ACCEPTED"] = "f"
options.headers["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.10; rv:39.0)"
    req = requests.get(shp_file_path, headers=options.headers, verify=True, allow_redirects=True)
    if req.ok:
        with open(local_file, 'wb') as file:
            file.write(req.content)

如果你没有得到auth_request并添加头文件,你就无法得到文件.

If you don't get the auth_request and add the headers, you can't get the file.

希望它在未来对我有用!任何改进都非常受欢迎!!:)

Hope it helps somebody in the future as worked for me! Any improvement is more than welcome!! :)

这篇关于用 Python 下载 Sharepoint Excel 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆