简体   繁体   English

上传文件到 Windows Sharepoint 使用 Python 3?

[英]Upload File to Windows Sharepoint using Python 3?

I have a test.txt on my Desktop and now I want to upload it to a Sharepoint Directory via Python3.我的桌面上有一个 test.txt,现在我想通过 Python3 将它上传到 Sharepoint 目录。 How can I do that?我怎样才能做到这一点?

I'll start by saying this example is adapted from the example for Office365-REST-Python-Client.首先我要说这个例子改编自 Office365-REST-Python-Client 的例子。 It works with sharepoint online using the rest api.它使用 rest api 在线与 sharepoint 合作。

https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/files/upload_file.py https://github.com/vgrem/Office365-REST-Python-Client/blob/master/examples/sharepoint/files/upload_file.py

Example url you might want to upload to [baseurl][site][folder][file].示例 url 您可能想要上传到 [baseurl][站点][文件夹][文件]。 https://your_company.sharepoint.com/path/to/site/Shared Documents/file.txt https://your_company.sharepoint.com/path/to/site/Shared Documents/file.txt

from office365.runtime.auth.authentication_context import AuthenticationContext
from office365.sharepoint.client_context import ClientContext

baseurl = 'https://your_company.sharepoint.com'
basesite = '/path/to/site' # every share point has a home.
siteurl = baseurl + basesite 

localpath = ./file.txt
remotepath = Shared Documents/file.txt # existing folder path under sharepoint site.

ctx_auth = AuthenticationContext(url)
ctx_auth.acquire_token_for_user(username, password)
ctx = ClientContext(siteurl, ctx_auth) # make sure you auth to the siteurl.

with open(localpath, 'rb') as content_file:
    file_content = content_file.read()

dir, name = os.path.split(remotepath)
file = ctx.web.get_folder_by_server_relative_url(dir).upload_file(name, file_content).execute_query()```

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM