简体   繁体   English

使用 python 在 SharePoint 中上传文件

[英]Upload file in SharePoint using python

'm trying to upload a file using Python Script, when Run the code it gives me no Error but was not able to upload the file in my sharepoint folder.我正在尝试使用 Python 脚本上传文件,当运行代码时,它没有给我任何错误,但无法将文件上传到我的共享点文件夹中。

import requests
from shareplum import Office365
from config import config

# get data from configuration
username = config['sp_user']
password = config['sp_password']
site_name = config['sp_site_name']
base_path = config['sp_base_path']
doc_library = config['sp_doc_library']

file_name = "cat_pic.jpg"

# 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'})


# perform the actual upload
with open( file_name, 'rb') as file_input:
    try: 
        response = session.post( 
            url=base_path + "/sites/" + site_name + "/Shared%20Documents/Forms/AllItems.aspx/_api/web/GetFolderByServerRelativeUrl('" + doc_library + "')/Files/add(url='" 
            + file_name + "',overwrite=true)",
            data=file_input)
    except Exception as err: 
        print("Some error occurred: " + str(err))

config.py

config = dict()
config['sp_user'] = 'email'
config['sp_password'] = 'pass
config['sp_base_path'] = 'https://bboxxeng.sharepoint.com'
config['sp_site_name'] = 'TESTIAN'
config['sp_doc_library'] = 'Test'

This is the url of my sharepoint https://bboxxeng.sharepoint.com/sites/TESTIAN/Shared%20Documents/Forms/AllItems.aspx I've already created a folder in it named Test...这是我的共享点的网址 https://bboxxeng.sharepoint.com/sites/TESTIAN/Shared%20Documents/Forms/AllItems.aspx我已经在其中创建了一个名为 Test 的文件夹...

Thank you for answering my question.谢谢你回答我的问题。

Modify the code as below.修改代码如下。

response = session.post( 
            url=base_path + "/sites/" + site_name + "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/"+doc_library+"')/Files/add(url='" 
            + file_name + "',overwrite=true)",
            data=file_input)

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

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