如何使用python脚本将文件上传到sharepoint站点
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23696705/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload a file to sharepoint site using python script
提问by user3590460
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.
有没有办法使用python脚本在sharepoint站点上上传文件?我尝试安装 haufe.sharepoint,但它似乎在安装时无法获取 ntlm,而且我什至无法在没有安装 ntlm 的情况下使用连接器模块。
I've also tried just saving the excel file to the server location (so save it to directory like \server\sharepointsite\files instead of connecting via the URL) using openpyxl, but it looks like the file remains checked out after the file is saved..
我还尝试使用 openpyxl 将 excel 文件保存到服务器位置(因此将其保存到 \server\sharepointsite\files 之类的目录中,而不是通过 URL 连接),但看起来该文件在文件被签出后仍然存在保存了..
I would appreciate any help. Thanks!!
我将不胜感激任何帮助。谢谢!!
回答by Calanas
haufe.sharepoint
only works for sharepoint lists, but you probably need access to document libraries.
haufe.sharepoint
仅适用于共享点列表,但您可能需要访问文档库。
You should use Python Requestswith the help of Sharepoint's REST API.
您应该在 Sharepoint 的REST API的帮助下使用 Python请求。
If your sharepoint site doesn't support BasicAuth I recommend the requests_ntlm
package.
如果您的 sharepoint 站点不支持 BasicAuth,我推荐该requests_ntlm
软件包。
It didn't work for me due to other reasons, but maybe it helps you out a bit.
由于其他原因,它对我不起作用,但也许它对您有所帮助。
回答by Ragini Sharma
I have created a file in SharePoint site in python via rest api calls. Please find my code below.
我通过rest api调用在python中的SharePoint站点中创建了一个文件。请在下面找到我的代码。
def CreateHomePage():
server_relative_url = base_url+ '/_api/web/webinfos'
r1 = requests.get(server_relative_url, auth=HttpNtlmAuth(username, password), headers = headers, verify=True)
value = json.loads(r1.text)
for row in value['d']['results']:
if(row['Title'] == myvars['Site Name'].strip(' \t\n\r')):
Id= row['ServerRelativeUrl']
#Add Template and create file simultaneously
title = myvars['Site Name'].strip(' \t\n\r')
post_url = root_url +'GetFolderByServerRelativeUrl(\'/'+Id+'/Pages\')/Files/add(url=\'Home.aspx\',overwrite=true)'
r2 = requests.post(post_url, auth=HttpNtlmAuth(username, password), headers = headers, verify=True)
logger.debug("Creation of home page %d", r2.status_code)
回答by Mohanty.pyt
I think I might be a bit late in answering this question.
我想我回答这个问题可能有点晚了。
The following solution worked for me-
以下解决方案对我有用-
In the Sharepoint webpage, Go to Library Tools>> Library>> Open with Explorer Command( Its the tiny icon in the bottom right beside Connect to Office command.
在 Sharepoint 网页中,转到库工具>>库>>使用资源管理器命令打开(它是连接到 Office 命令旁边右下角的小图标。
The address bar gives us the address that we need to upload the file to. Remember to remove "http:" or "https:" from the addressThis address is your destination to upload the file.
地址栏为我们提供了将文件上传到的地址。请记住从地址中删除“http:”或“https:”该地址是您上传文件的目的地。
Subsequently you can use shutil package to upload the file.
随后您可以使用shutil 包上传文件。
import shutil as sl
sl.copy(source,destination)
This should help you upload files to Sharepoint
这应该可以帮助您将文件上传到 Sharepoint
Disclaimer- This works quite well in Python 3.6
免责声明 - 这在 Python 3.6 中运行良好
回答by Mikus
The answers above didn't work for me. I have found a simple and nice way by just mapping a drive to my sharepoint folder and then I used a copy to that drive.
上面的答案对我不起作用。我找到了一种简单而好的方法,只需将驱动器映射到我的共享点文件夹,然后我将副本复制到该驱动器。
import subprocess
import shutil
subprocess.call(r'net use Y: http://sharepoint/link/to/your/folder', shell=True)
shutil.copy("link_to_local_file","Y:\")
Instead of copy, You can also delete files or do anything like a normal folder.
除了复制,您还可以删除文件或执行任何类似于普通文件夹的操作。
回答by Michelone
I have created a script to upload attachment into a SharePoint list let me know if it works
我创建了一个脚本来将附件上传到 SharePoint 列表,请告诉我它是否有效
import requests
from shareplum import Office365
# Obtain auth cookie
authcookie = Office365('https://YOUR-NAME.sharepoint.com', username='YOUR-USERNAME',password='YOUR-PASSWORD').GetCookies()
session = requests.Session()
session.cookies = authcookie
session.headers.update({'user-agent': 'python_bite/v1'})
session.headers.update({'accept': 'application/json;odata=verbose'})
# dirty workaround.... I'm getting the X-RequestDigest from the first failed call
session.headers.update({'X-RequestDigest': 'FormDigestValue'})
response = session.post(url="https://YOUR-NAME.sharepoint.com/sites/YOU-SITE/_api/web/GetFolderByServerRelativeUrl('YOUR-FOLDER')/Files/add(url='a.txt',overwrite=true)",data="")
session.headers.update({'X-RequestDigest': response.headers['X-RequestDigest']})
# perform the upload
fileName = 'picture.png'
file_name = 'images.png'
with open(file_name, 'rb') as file_input:
response = session.post(
url="https://YOUR-NAME.sharepoint.com/sites/YOUR-SITE/_api/web/lists/getbytitle('NAME-LIST')/items(4)/AttachmentFiles/add(FileName='" + fileName + "')",data=file_input)
print(response.text)