
Introduction
This article explains how to upload your files to Microsoft SharePoint using Python.
What is a SharePoint?
SharePoint in Microsoft 365 A cloud-based service, hosted by Microsoft, for businesses of all sizes. Instead of installing and deploying the SharePoint Server on-premises, any business can subscribe to a Microsoft 365 plan or to the standalone SharePoint Online service. Your employees can create sites to share documents and information with colleagues, partners, and customers.*
We will be using the python office365 SDK client to interact with the share point.
Work Flow:
- 
Install Office365 Python client 
- 
Initiate the authentication flow 
- 
Create the upload directory in SharePoint 
- 
Upload file(s) 
Install Office365 python client:
$pip install Office365-REST-Python-Client
GitHub - vgrem/Office365-REST-Python-Client: Office 365 & Microsoft Graph Library for Python
Create a SharePoint client context object:
To work with SharePoint APIs, we need to create a ClientContext object. This can be done with either:
- 
username and password or 
- 
the client id and client secret. 
For most of the real-time use cases, we may not be using username and password. Therefore, in this article, we will create the context object using the client id and secret.
Bonus: The client id and secret can preferably be stored in a vault or any convenient way as you do in your project. If you'd like to have a look at storing secrets in vault, please read this article.
i) Using app credentials:
from office365.runtime.auth.client_credential import ClientCredential
from office365.sharepoint.client_context import ClientContext
def get_sharepoint_context_using_app():
    # Get sharepoint credentials
    sharepoint_url = 'https://{your-tenant-prefix}.sharepoint.com'
    # Initialize the client credentials
    client_credentials = ClientCredential(<your client id>, <your secret>)
    # create client context object
    ctx = ClientContext(sharepoint_url).with_credentials(client_credentials)
    return ctx
ii) Using user credentials:
from office365.runtime.auth.user_credential import UserCredential
from office365.sharepoint.client_context import ClientContext
def get_sharepoint_context_using_user():
    # Get sharepoint credentials
    sharepoint_url = 'https://{your-tenant-prefix}.sharepoint.com'
    # Initialize the client credentials
    user_credentials = UserCredential(<your username>, <your password>)
    # create client context object
    ctx = ClientContext(sharepoint_url).with_credentials(user_credentials)
    return ctx
Now that we have received the context object, let’s start interacting with the SharePoint APIs.
Create a share point directory using python:
To upload files to SharePoint, let us first create a SharePoint directory. The below code creates a directory in the share point server inside the Documents folder.
Note: Please note that the documents folder is named Shared Documents in the relative_url.
def create_sharepoint_directory(dir_name: str):
    """
    Creates a folder in the sharepoint directory.
    """
    if dir_name:
        ctx = get_sharepoint_context_using_app()
        result = ctx.web.folders.add(f'Shared Documents/{dir_name}').execute_query()
        if result:
            # documents is titled as Shared Documents for relative URL in SP
            relative_url = f'Shared Documents/{dir_name}'
            return relative_url
create_sharepoint_directory('test directory')
Output:
Shared Documents/test directory
Upload multiple files to Share point:
The below method uses ThreadPoolExecutor to upload multiple files to SharePoint. The upload_file_lst is a list of files. We get the folder URL from the context object using the relative_url of the directory we created in our previous step.
Finally from the future result object, we call execute_query() to complete the upload.
from concurrent.futures import ThreadPoolExecutor, as_completed
def upload_to_sharepoint(dir_name: str, upload_file_lst: list):
    sp_relative_url = create_sharepoint_directory(dir_name)
    ctx = get_sharepoint_context()
    target_folder = ctx.web.get_folder_by_server_relative_url(sp_relative_url)
    with ThreadPoolExecutor(max_workers=5) as executor:
        for file_name in upload_file_lst:
            with open(file_name, 'rb') as content_file:
                file_content = content_file.read()
                futures = executor.submit(target_folder.upload_file, file_name, file_content)
                if as_completed(futures):
                    futures.result().execute_query()
Upload a single file to SharePoint:
def upload_to_sharepoint(dir_name: str, file_name: str):
    sp_relative_url = create_sharepoint_directory(dir_name)
    ctx = get_sharepoint_context()
    target_folder = ctx.web.get_folder_by_server_relative_url(sp_relative_url)
    with open(file_name, 'rb') as content_file:
        file_content = content_file.read()
        target_folder.upload_file(file_name, file_content).execute_query()
Upload multiple files to SharePoint with the progress bar:
Now that we have seen the upload of single/multiple files to share point, we will extend the multiple file upload code a little and add a progress bar to show the upload.
from concurrent.futures import ThreadPoolExecutor, as_completed
def upload_to_sharepoint(dir_name: str, upload_file_lst: list):
    sp_relative_url = create_sharepoint_directory(dir_name)
    ctx = get_sharepoint_context()
    target_folder = ctx.web.get_folder_by_server_relative_url(sp_relative_url)
    no_of_upload_files = len(upload_file_lst)
    with tqdm(total=no_of_upload_files, desc="Uploading..", initial=0, unit_scale=True, colour='green') as pbar:
        with ThreadPoolExecutor(max_workers=5) as executor:
            for file_name in upload_file_lst:
                with open(file_name, 'rb') as content_file:
                    file_content = content_file.read()
                    futures = executor.submit(target_folder.upload_file, file_name, file_content)
                    if as_completed(futures):
                        futures.result().execute_query()
                        pbar.update(1)
To learn more about showing progress bars during upload/download, please refer to this article.
Summary
- We have used Office365 Python SDK to interact with share point. We could very well do the same with other Microsoft products like MS Outlook, OneDrive, Teams, etc.
- We have 2 types of auth flows to create a context object, app credentials, and user credentials.
- Create a context object and use the same to perform any operation in SharePoint.
Originally published at https://dock2learn.com on April 25, 2022.



