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

查看:813
本文介绍了在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"

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的测试文件夹中找到了一个示例.因此,基本上,在获取带有请求的网址之前,您需要获得授权:

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天全站免登陆