如何将文件从我的目录上传到SharePoint

时间:2018-01-16 22:25:27

标签: python python-2.7 sharepoint

我正在尝试将计算机上某个目录中的所有项目上传到SharePoint网站。是否可以创建一个执行此任务的脚本?

2 个答案:

答案 0 :(得分:1)

这可能不是这个问题的网站。

但是......如果您在Office 365上使用Sharepoint,则可以将sharepoint库的内容同步到您的计算机。然后,您放入文件夹的任何文档都会上传到您的sharepoint站点。

Sync instructions

答案 1 :(得分:0)

当然,您可以通过创建放置请求将整个文件夹上传到Sharepoint。我写了下面的代码,将目录上传到Sharepoint服务器。您必须提供用户身份验证,检查文件夹中的文件是否要上传,并输入('y'或'n'不区分大小写)以及在Sharepoint服务器上上传数据所需的完整路径。服务器响应输出应为201或200 - 确定正确上传文件。

import requests
import os
import sys
from requests_ntlm import HttpNtlmAuth
import glob

path = 'C:\Python27' #CHANGE THIS it is an upload path where the files are stored


files = next(os.walk(path))[2]
print "File array:"
print files
print "\n"
print 'Total number of files in', path, 'is', len(files)
print "\n"
print "Files listed in directory:"
print "\n"
i = 0
for i in range(0,len(files)):
    print(path+"\\."+files[i])
    i += 1
print "\n"

status = []

play = True

while play:

    answer = raw_input('Do you want to continue to upload corresponding files? (Y)es or (N)o: \n').lower()

    while True:
        if answer == 'y':

        print 'Script will start uploading... \n'

        print 'Check if status codes are 200 (200 OK - The request has succeeded) ' 
        print 'or 201 (201 CREATED - The request has been fulfilled and has resulted'
        print 'in one or more new resources being created). If not, try again. \n'

        for i in range(0, len(files)):

            filename = files[i]

            session = requests.Session()

            session.auth = HttpNtlmAuth('SharepointDomain\\username','password', session) #CHANGE THIS

            file = open(path + "\\" + filename, 'rb')

            bytes = bytearray(file.read())

            resp = requests.put('Full directory path including hostname where the files will be uploaded' + filename, data=bytes, auth=session.auth)

            print "Status response for file #",i+1, "is", resp.status_code

            status = 'ok'

        break

        elif answer == 'n':
            play = False
            break
        else:
            answer = raw_input('Incorrect input. Press \"Y" to continue or \"N" to leave": ').lower()



print 'Program will exit.'
相关问题