Python - 从 SharePoint 站点下载文件 [英] Python - Download files from SharePoint site
问题描述
我需要下载文件并将其上传到 Sharepoint 站点.这必须使用python来完成.我的网站将作为 https://ourOrganizationName.sharepoint.com/Followed 通过进一步链接最初我认为我可以使用 Request、BeautifulSoup 等来做到这一点,但我根本无法进入网站正文中的检查元素".
I have a requirement of downloading and uploading the files to Sharepoint sites. This has to be done using python. My site will be as https://ourOrganizationName.sharepoint.com/Followed by Further links Initially I thought I could do this using Request, BeautifulSoup etc., But I am not at all able to go to "Inspect Element" on the body of the site.
我尝试过 Sharepoint、HttpNtlmAuth、office365 等库,但没有成功.它总是返回 403.
I have tried libraries such as Sharepoint,HttpNtlmAuth,office365 etc., but I am not successful. It always returning 403.
我尽可能多地尝试了谷歌,但还是没有成功.甚至 Youtube 也没有帮助我.
I tried google as much I can but again not successful. Even Youtube hasn't helped me.
有人能帮我怎么做吗?非常感谢有关带有文档链接的库的建议.
Could anyone help me how to do that? Suggestion on Libraries with documentation link is really appreciated.
谢谢
推荐答案
你试过 Office365-REST-Python-Client
库,支持SharePoint Online 身份验证,并允许下载/上传文件,如下所示:
Have you tried Office365-REST-Python-Client
library, it supports SharePoint Online authentication and allows to download/upload a file as demonstrated below:
下载文件
from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext
from office365.sharepoint.files.file import File
ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(url, ctx_auth)
response = File.open_binary(ctx, "/Shared Documents/User Guide.docx")
with open("./User Guide.docx", "wb") as local_file:
local_file.write(response.content)
上传文件
ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(url, ctx_auth)
path = "./User Guide.docx" #local path
with open(path, 'rb') as content_file:
file_content = content_file.read()
target_url = "/Shared Documents/{0}".format(os.path.basename(path)) # target url of a file
File.save_binary(ctx, target_url, file_content) # upload a file
使用
安装最新版本(来自 GitHub):
Install the latest version (from GitHub):
pip install git+https://github.com/vgrem/Office365-REST-Python-Client.git
参考 file_operations.py
了解更多详情
Refer file_operations.py
for a more details
这篇关于Python - 从 SharePoint 站点下载文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!