Python将文件和文件夹上传到共享点

时间:2020-04-08 13:29:49

标签: python-3.x file sharepoint file-upload

我正在尝试将一些文件(多个文件和文件夹)上载到共享点,当我运行脚本时,我没有任何错误,但是我无法看到共享点中的文件。

import requests
from shareplum import Office365

# get data from configuration
username = 'last.surname@tenant.com'
password = 'mypassword'
site_name = 'BI_odair'
base_path = 'https://tenant.sharepoint.com'
doc_library = 'data'

file_name = "links.txt"

# 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 +
            "/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents/" +
            doc_library+"')/Files/add(url='"
            + file_name + "',overwrite=true)",
            data=file_input)
    except Exception as err:
        print("Some error occurred: " + str(err))

我的共享点的网址:https://tenant.sharepoint.com/sites/BI_odair/Documents%20partages/Forms/AllItems.aspx?viewid=4e7fdfb9%2De84a%2D42cd%2Db537%2D0d2837ca92cc 已经有一个名为Data的文件夹,但是我想将我的文件上传到根文件夹“ / Documents%20partages”中

我已经将其添加到我的代码中:https://stackoverflow.com/a/59083429/6754555

谢谢。

2 个答案:

答案 0 :(得分:0)

“共享文档”是默认的文档库。如果要将文件上传到自定义库,请修改路径,如下所示:

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

您还可以浏览以下博客:Uploading files to SharePoint

////////更新

我创建了一个文档库并在上面的博客中测试了该演示,它运行良好。 enter image description here

下面是我的代码:

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 = "test.csv"

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

# dirty workaround.... I'm getting the X-RequestDigest from the first failed call
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']})

# perform the actual upload
with open( r'C:\Users\xxx\Documents\test.csv', 'rb+') as file_input:
    try: 
        response = session.post( 
            url=base_path + "/sites/" + site_name + "/_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))

print('end...')

enter image description here

答案 1 :(得分:0)

这是对我来说完美的解决方案:

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

# perform the actual upload
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')