Efficient scripting with Python & SharePoint in Microsoft Office 365
A Python script to automate uploading to Sharepoint
Python scripting is a great way to automate many processes that you use daily such as updating an excel spreadsheet with data from an API, sending out daily PDF reports, or uploading files to a google cloud.
Computers are incredibly efficient at carrying out repeated tasks, and we can utilize this power with scripts and cron jobs. For example, when regularly uploading a Python-produced report or CSV file on a Monday Morning to Sharepoint/Onedrive to provide updated progress reports.
Authentication Authenticating with a password and user email is necessary to access the shared file directory in SharePoint sites. Which can prove to be tricky if your account has 2-factor authentication enabled; this will raise an Exception:
Exception('Error authenticating against Office 365.')
So for this to work and bypass any authentication errors, you can create a new password specifically for executing the script.
Creating a new App Password in Microsoft 365 Log into your Office 365 account → View Account → Security info| You should see the methods you use to sign in to your account. You can then click 'Add method.'

A pop-up modal should appear, and then choose 'App Password' for this to work.

Next, Microsoft will prompt you to create a password specifically for this app, and then you're all done. The Password can be stored in Github's secret vault & used in the script to authenticate against your SharePoint site.
Creating the Python Script
Libraries The two libraries you will need for this are requests for authentication and session data, which is inbuilt with Python, and the Shareplum library.
Shareplum is a python library that enables access to the Office 365 suite in your python code, alongside File & Folder uploads through the Office 365 API.

Then, we can create two separate functions:
- Authentication function — passes in user credentials to office 365 and return the site & auth cookie.
- Upload files — does what it says on the tin.
You can also import the latter function into other files for use externally e.g. from export_sharepoint import upload365 .

You can then call this authenticate_office365() in a secondary function, which takes an input of the file you want to upload, the file's name, and any other information that might be necessary.

The code above creates a new function that calls the previous authentication for loading the Sharepoint site and necessary cookies to performing actions in the session. Creating a session allows us to store information for use across multiple web pages.
The inbuilt open() function can be used to read any file content before we finally use the upload_file function which is a method associated with the site folder we previously accessed through Shareplum's Library.
And there you have it, in a few lines of code, we're able to access Sharepoint sites & upload or update and files needed for regularly scheduled reporting. I hope this helps anyone looking for an easy way to upload to Microsoft services and kickstart their day!




