Uploading files to SharePoint using Python – Part 2

Python SharePoint

In this multi part article, we are going to setup a Python application that will upload documents to a SharePoint Document Library. We will be using an open source Python module named Shareplum to help simplify some parts of this. 

Introduction

This tutorial is going to be broken into 3 parts. In part 1 we will go through the steps to create a new virtual environment, install the dependencies and authenticating with SharePoint. Part 2 we will work on uploading documents and adding metadata to an uploaded document. Then in part 3 will put all this together and iterate over a folder containing multiple files and add metadata to each file bases on an index file or the name of the file. 

I will be working with Visual Studio Code in a Windows environment for this tutorial. All the step include here will work with any IDE, however, some may need to be completed in a different manner.

Interacting with SharePoint using Python – Part 1

Interacting with SharePoint using python – Part 3 

Add a File to A SharePoint Document Library

Now that we can log into SharePoint, we are going to upload a file. Keeping the modularity in mind for the end-product, we are going to create a new file named “uploadfile.py”. This files code will handle all the uploads in the future.

First, we need to import our “Auth” class from the authertication.py file. We will also import “Site” from Shareplum.

from authentication import Auth
from shareplum import Site

Now we will create a new class named “Upload”. When we call the Upload class will pass it four arguments: site, filePath, fileName, sharePointFolder. This class will only have two functions, the dunder init and the “filePayload” functions. Just like the “Auth” class, the dunder init function will initialize our vaiables and call our filePayload functions. The filePayload function creates a connection to the SharePoint document library that is specified when calling the Upload class along with the “site” we get from the “Auth” class. Then we use “with open” to open the file as read-only in binary format. Then we upload the files binary content to the SharePoint document library and name it the original file name.

class Upload:
    # initiate the variables and call filePayload
    def __init__(self, site, filePath, fileName, sharePointFolder):
        self.site = site
        self.filePath = filePath
        self.fileName = fileName
        self.sharePointFolder = sharePointFolder
        self.filePayload()
        
    #Build the file payload
    def filePayload(self):
        folder = self.site.Folder(self.sharePointFolder)
        with open(f'{self.filePath}\\{self.fileName}', 'rb') as f:
            file_content = f.read()

        folder.upload_file(file_content, self.fileName)
Join Amazon Prime – Watch Thousands of Movies & TV Shows Anytime – Start Free Trial Now

To test that files can be uploaded you can add the following code to uploadfile.py. If the upload was successful you should see the file name in the large dictionary that is returned.

login = Auth('username@domain.com', 'password', 'https://username.sharepoint.com/sites/Test', 'https://domain.sharepoint.com')

    Upload(login.site, r'C:\path\to\folder', 'test.pdf', 'document library name to upload to')

    list_data = login.site.List('document library name to upload to')

    print(list_data.GetListItems())

Update the Meta data of the File to Your SharePoint Document Library

Now that we can authenticate to SharePoint and upload a file, we need to add some meta data to the file. We are going to look at two different ways to do this. The first will read a csv file that contains the meta data. The second way will be by parsing the files name for the meta data. This is used for documents being scanned to a network folder, the person doing the scanning then renames the file, i.e. metadata-metadata2.pdf. 

First, we are going to make a new file name indexing.py. This file will contain the Indexing class. This class has two functions. One for creating keywords and one for reading an index file. Each function will return a list of keywords. I am sure that there is some one-liner out there that could accomplish my four lines. If you know of this, please leave a comment below.

class Indexing:
    def createKeywords(self, folderPath, file):
        file_name_list = file.split('-')
        no_extintion = file_name_list[-1].split('.')        
        file_name_list[-1] = no_extintion[0]  
        keywordList = file_name_list[0:len(file_name_list)]   

        return keywordList

    #reads the index file and creates a list
    def readIndex(self, file, folderPath):                
        with open(f'{folderPath}\\{file}', 'r') as f:
            file_content = f.read()
            indexList = file_content.split(',')
            
            return indexList

Now that we have the keywords, we need to make dictionary to call the Shareplum UpdateListItems function. To do this we will need to make a new file named util.py. In this file we will add a new class named Util. This class will have two functions. One to create a dictionary from 2 lists and the other will merge 2 dictionaries.

class Util:    
    def mergeDict(self, dict1, dict2):
        res = {**dict1, **dict2}
        return res

    def createDict(self, customeColumnList, keywordsList):
        res = dict(zip(customeColumnList, keywordsList))
        return res

Putting It Altogether

At this point me are going to need a way to put all this together. In the next part we will create a main file to house all the code to do this. This will allow us to pull in some of the variables from a config file, check a folder to see if it has files, loop through file in the folder and upload each one.

1 thought on “Uploading files to SharePoint using Python – Part 2

Leave a Reply