Python Bite - upload file to Sharepoint Online with Python

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hey guys so in this video I'm gonna show you how to upload files to SharePoint using Python so our tasks at hand is to upload this cat picture to fair point the main script is composed out of couple of sections so first of them is actually getting the configuration from the config file which I will show you in a second then obtaining the authorization cookie then I did two workaround to get the X request digest header I don't have a better way to do it yet so there is this abductee workaround and then the actual upload so let's get into the details so I'm gonna show you the template so the template looks like this this is the configuration file which is a simple dictionary and then it composed it's composed out of sharpened user SharePoint password the base path so like this without the slash sharpen cycling in the direction the the document library that you would like to upload this this file into so so it looks like this so I have a title uploaded folder it's on you know SharePoint document library we have any files in it and then I can the goal is when I will run my script I want this file this picture to be actually uploaded to this folder so let's go through the script itself so this is the template of the configuration and in the first section I'm reading this configuration to local variables so first of course there are imports so import three quests we're going to use this there is a sharp LOM I'm using so there is a package called char plum and I'm using office 365 above it the the the class called office365 which I'm using to authenticate with office 365 SharePoint it's not mandatory you can authorize using as well rest rest api on cover since it was already done I decided not to redo this code then I've kind of have us well my config file so I'm importing this like this so first getting the configuration into the specific Python Python variables then my file name I called it can't pick and it's in the same folder as the rest of my scripts and I'm on so first step getting the authorization cookie so I'm actually using the office 365 package and I'm providing the base path to the SharePoint the username password and a function called get cookies this will give me after ization cookie that I'm now adding to my session so I'm creating a session with from requests and I'm assigning my authorization cookie to take sessions cookies I need to now as well update the headers of my session some I'm writing the user agent header and except some exact accepting application JSON all data proposed and user agent you can actually put whatever doesn't really matter then there is this dirty work around I haven't figured out yet awake how to get the X request digests header from the server from the SharePoint without making the actual call and failing so what it actually does it makes a post request to the SharePoint which fails because the extras digest response is not set however this failed response contains a proper X because digest header so I'm actually assigning X rituals like this color from the response that I got from this failed post and then finally I'm performing the actual upload which means open the file name as input file response post and the URL is base URL science site name then there is an API so get folder by server related URL and my document library files and URL and after the role is the final and if I want to override yes so if I will have a new file it will be overwritten on the SharePoint and the data as a parameter of the post is the the content of the file input and if there are some exceptions exceptions I'm raising them here and let's actually now see if this works so first I'm showing the Louis's empty so let me refresh just to make sure it's fully up to date so you can see the folder is empty and now let me run the script so just to show you I have this config should have put upload the cat picture inside and as well this convict template that you've seen of course my user and password are hidden in the config so I want to share this with you guys but let's see if it works so here and Sharaf on upload it works for a few seconds and it's done and now let's see if my yeah and I can see already my file is updated just a few seconds ago so if I click on the cat picture let's see if it's yep it works so I have my cat picture uploaded to SharePoint using Python okay you will get the code from my blog and if you have any requests to have an interesting Python videos just let me know and I'll try to record them if you need some help or you would have problem isn't potentially solvable by Python let me know and I will try to figure it out if it's possible and if yes I will record a video and sure the code with you guys Thanks
Info
Channel: Python Bites
Views: 45,342
Rating: undefined out of 5
Keywords: python, sharepoint, file upload
Id: OY7csyqO3Zc
Channel Id: undefined
Length: 6min 17sec (377 seconds)
Published: Thu Jul 11 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.