如何使用python脚本将文件上传到sharepoint站点 [英] How to upload a file to sharepoint site using python script

查看:69
本文介绍了如何使用python脚本将文件上传到sharepoint站点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Is there a way to upload a file on sharepoint site using python script? I tried installing haufe.sharepoint, but it seems like it failed to fetch ntlm while it was installing, and I can't even use the connector module without having ntlm installed.

我还尝试使用 openpyxl 将 excel 文件保存到服务器位置(因此将其保存到 serversharepointsitefiles 之类的目录中,而不是通过 URL 连接),但看起来该文件在之后仍被签出文件已保存..

I've also tried just saving the excel file to the server location (so save it to directory like serversharepointsitefiles instead of connecting via the URL) using openpyxl, but it looks like the file remains checked out after the file is saved..

我将不胜感激.谢谢!!

I would appreciate any help. Thanks!!

推荐答案

我首先要说的是,这个示例改编自 Office365-REST-Python-Client 的示例.它使用rest api在线共享点.

I'll start by saying this example is adapted from the example for Office365-REST-Python-Client. It works with sharepoint online using the rest api.

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

Example url you might want to upload to [baseurl][site][folder][file]. 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()

这篇关于如何使用python脚本将文件上传到sharepoint站点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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