avatarArcade

Summary

The web content provides a guide on using Python and SharePlum to automate file uploads to SharePoint in Microsoft Office 365, including handling authentication with app passwords.

Abstract

The article outlines an efficient method for automating file uploads to SharePoint within Microsoft Office 365 using Python scripting. It emphasizes the power of computer automation for repetitive tasks, such as updating SharePoint with reports or CSV files. The guide addresses the challenge of authentication, particularly with two-factor authentication, by suggesting the creation of an app password in Microsoft 365. It walks through the process of generating this password and storing it securely. The article also introduces the necessary Python libraries, requests and SharePlum, for interacting with the Office 365 API. It details the creation of two functions: one for authentication and another for uploading files, which can be reused across different scripts. The use of environment variables to handle sensitive information is recommended, and the article concludes by demonstrating how to implement the upload functionality.

Opinions

  • The author believes that Python scripting is an excellent tool for automating daily tasks, such as file uploads to SharePoint.
  • Authentication with 2-factor authentication can be tricky, and the author suggests creating a dedicated app password as a workaround.
  • The author advocates for the use of Github's secret vault to securely store the app password used in the script.
  • The SharePlum library is recommended for its ability to facilitate access to Office 365 and handle file uploads through the Office 365 API.
  • The author values modularity and reusability in scripting, as evidenced by the suggestion to import the upload function into other files for external use.
  • The use of environment variables, such as os.environ["SECRET_KEY"], is encouraged for managing sensitive credentials within scripts.
  • The author is optimistic that the provided instructions will help users streamline their reporting processes by automating file uploads to Microsoft services.

Efficient scripting with Python & SharePoint in Microsoft Office 365

A Python script to automate uploading to Sharepoint

Photo by Johny vino on Unsplash

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:

  1. Authentication function — passes in user credentials to office 365 and return the site & auth cookie.
  2. 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 .

Note that for the Password, we are using os.environ["SECRET_KEY"], and you can find more information on this at https://medium.datadriveninvestor.com/accessing-github-secrets-in-python-d3e758d8089b

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.

Creating the upload files function

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!

Python
Microsoft
Scripting
Sharepoint
Recommended from ReadMedium