29

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.

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..

I would appreciate any help. Thanks!!

user3590460
  • 371
  • 1
  • 3
  • 7

7 Answers7

18

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

Example URL you might want to upload to [baseurl][site][folder][file]. https://your_company.sharepoint.com/path/to/site/Shared Documents/file.txt

import os
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(siteurl) # should also be the siteurl
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()
G. LC
  • 794
  • 1
  • 8
  • 27
Josh
  • 482
  • 5
  • 12
6

haufe.sharepoint only works for sharepoint lists, but you probably need access to document libraries.

You should use Python Requests with the help of Sharepoint's REST API.

If your sharepoint site doesn't support BasicAuth I recommend the requests_ntlm package.

It didn't work for me due to other reasons, but maybe it helps you out a bit.

DocZerø
  • 8,037
  • 11
  • 38
  • 66
Calanas
  • 61
  • 1
  • 2
5

You could upload files with SharePlum

install SharePlum: pip install SharePlum and try the code below

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

# Upload file
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')
nurealam siddiq
  • 1,567
  • 10
  • 9
  • Worked for me. Thank you for this Answer Nurealam. But 1. How do I specify the nested_folder which is not created and expect it to save by creating the nested folder if it does not exist. 2. Is this method a safe way to send the request over the internet ? – shreesh katti Apr 09 '21 at 10:46
  • 1. You might want to create a folder separately before defining the nested folder (check here how to create a folder: https://pypi.org/project/SharePlum/), the above code doesn't create folders automatically and you will get an error. 2. Yes it is safe, I wouldn't use otherwise. You also could use client_secret and client_id if you are not comfortable to use it @shreeshkatti. – nurealam siddiq Apr 10 '21 at 11:03
  • Note: You might not have access to `abc.sharepoint.com`, you can access your own environment at `abc-my.sharepoint.com`. – Krishna May 24 '21 at 10:16
3

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.

The address bar gives us the address that we need to upload the file to. Remember to remove "http:" or "https:" from the address This address is your destination to upload the file.

Subsequently you can use shutil package to upload the file.

import shutil as sl
sl.copy(source,destination)

This should help you upload files to Sharepoint

Disclaimer- This works quite well in Python 3.6

Mohanty.pyt
  • 81
  • 1
  • 4
2

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.

Mikus
  • 61
  • 2
  • This does not work for me. What is "net use Y:" ?!? – mah65 Nov 11 '19 at 21:51
  • @Mikus I got this error, when I tried with your suggestion.. FileNotFoundError: [Errno 2] No such file or directory: 'Y:\\' – Idleguys Jul 08 '20 at 22:35
  • Waouh, so simple. This just maps a local hard drive to the remote sharepoint! No more needs of python library or sharepoint API :) – etienne-sf Jun 02 '23 at 07:24
0

I have created a file in SharePoint site in python via rest api calls. Please find my code below.

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)
andreas
  • 16,357
  • 12
  • 72
  • 76
0

I have created a script to upload attachment into a SharePoint list let me know if it works

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('ID-ROW-INTO-SHAREPOINT')/items(4)/AttachmentFiles/add(FileName='" + fileName + "')",data=file_input)
    print(response.text)
Michele
  • 31
  • 4