How to upload file to sharepoint site using python script

Is there a way to upload a file to a sharepoint site using a python script? I tried installing haufe.sharepoint, but it looks like it was unable to get ntlm during installation, and I cannot even use the connector module without installing ntlm.

I also tried just saving the excel file to the server location (so save it in a directory like \ server \ sharepointsite \ files, instead of connecting via the url) using openpyxl, but it looks like the file remains extracted after the file is saved.

I would be grateful for any help. Thanks!!

+8
python sharepoint openpyxl
source share
4 answers

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

You must use Python Requests with the Sharepoint REST API .

If your sharepoint site does not support BasicAuth, I recommend the requests_ntlm package.

This did not work for me for other reasons, but maybe it helps you a bit.

+4
source share

I think I can be a little late with the answer to this question.

The following solution worked for me-

On the Sharepoint web page, select "Library Tools"> "Library" >> "Open with the Explorer command" (this is a small icon in the lower right corner next to the "Connect to Office" command).

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

Subsequently, you can use the shutil package to download the file.

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

This should help you upload files to Sharepoint.

Disclaimer- This works well in Python 3.6

+1
source share

I created a file on a 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) 
0
source share

The answers above did not help me. I found a simple and enjoyable way by simply connecting the drive to my sharepoint folder and then using a copy on 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 copying, you can also delete files or do something like a regular folder.

0
source share

All Articles