如何使用python脚本将文件上传到sharepoint站点

use*_*460 11 python sharepoint python-2.7 openpyxl

有没有办法使用python脚本在sharepoint站点上传文件?我尝试安装haufe.sharepoint,但它似乎无法在安装时获取ntlm,我甚至无法在没有安装ntlm的情况下使用连接器模块.

我还尝试将excel文件保存到服务器位置(因此将其保存到目录,如\ server\sharepointsite\files而不是通过URL连接)使用openpyxl,但看起来该文件在文件后保持签出状态保存..

我将不胜感激任何帮助.谢谢!!

小智 5

haufe.sharepoint仅适用于共享点列表,但是您可能需要访问文档库。

您应该在Sharepoint的REST API的帮助下使用Python 请求

如果您的共享点站点不支持BasicAuth,则建议使用该requests_ntlm软件包。

由于其他原因,它对我不起作用,但也许可以帮到您一点。


小智 5

我首先要说这个示例改编自 Office365-REST-Python-Client 的示例。它使用rest api在线与sharepoint一起使用。

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

您可能想要上传到 [baseurl][site][folder][file] 的示例 url。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()
Run Code Online (Sandbox Code Playgroud)

  • 使用“pip install office365-rest-client”安装该软件包。`pip install office365` 不起作用。详细信息请参见此处,https://github.com/vgrem/Office365-REST-Python-Client/issues/255 (4认同)

nur*_*diq 5

您可以使用 SharePlum 上传文件

安装 SharePlum:pip install SharePlum并尝试下面的代码

import requests
from shareplum import Office365

# Set Login Info
username = '<username>'
password = '<password>'
site_name = '<site_name>'
base_path = 'https://<domain_name>.sharepoint.com'
doc_library = 'Shared%20Documents'
nested_folder = 'Shared%20Documents/<folder1>/<folder2>' #if you want to upload in nested folders else nested_folder = doc_library
file_name = "my_file.zip" #when your file in the same directory

# Obtain auth cookie
authcookie = Office365(base_path, username=username, password=password).GetCookies()
session = requests.Session()
session.cookies = authcookie
session.headers.update({'user-agent': 'python_bite/v1'})
session.headers.update({'accept': 'application/json;odata=verbose'})

session.headers.update({'X-RequestDigest': 'FormDigestValue'})
response = session.post(url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='a.txt',overwrite=true)",
                         data="")
session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})

# Upload file
with open(file_name, 'rb') as file_input:
    try:
        response = session.post(
            url=base_path + "/sites/" + site_name + f"/_api/web/GetFolderByServerRelativeUrl('" + nested_folder + "')/Files/add(url='"
            + file_name + "',overwrite=true)",

            data=file_input)
        print("response: ", response.status_code) #it returns 200
        if response.status_code == '200':
            print("File uploaded successfully")
    except Exception as err:
        print("Something went wrong: " + str(err))

print('File Uploaded Successfully')
Run Code Online (Sandbox Code Playgroud)