Python Download File from SharePoint

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what up everybody i am lou and today we're going to be talking about how to download files from sharepoint so i actually been getting a lot of views on on my sharepoint python videos so man appreciate the love thanks for watching uh i mean i'm hoping that you know the videos i'm making are helping somebody out um if they are man give me thumbs up follow me much appreciated man much appreciated for real um but i got a request about hey can you create a video on how to download uh from sharepoint uh i think i believe they're having some issues downloading so i'm gonna go ahead and walk you through and let's go through the process it's really not that hard it's pretty simple um i have a code base that i probably could use in my other sharepoint projects only reason i'm not adding on to that is because i know we may have some new viewers that haven't seen the other videos so i'm kind of just gonna start fresh from the beginning uh but nevertheless though if you see my other videos the process is pretty much the same there's only some small tweaks again just a method for downloading and then of course writing it out but let's go ahead and walk through it and and let's get started again we're going to be using python share plump so if you don't have it go ahead and get that installed also those who are new get familiar with you know virtual environments what you don't want what you do not want to do is just start installing everything on your main interpreter that you have on your desktop you want to create virtual environments not on your main environment and it'll save you a lot of headaches so again you're not familiar with it go get familiar with virtual environments but let's go ahead and get started so first thing i'm going to do just like my other projects i'm going to create a sharepoint file i'm going to create a config json file and i'm going to create um a project file and again you'll see in a minute how everything will you know will come together so let's start off with the json config file so this is ultimately how it's going to look now that's how i have mine set up again you don't have to do it the way i did ideally again in a production environment you would have variable file env or you would have just variables on your actual server um that you would end up calling that will bring in these configurations but nevertheless for the purpose of what we're doing i have the config file um username is going to be your email for again everything is office 365. um i'm not doing any on-prem just you know um office 365. uh username password your url so you should have some sort of domain even if you don't have um an actual domain but you just kind of use you know in the microsoft generated version you still have something there whatever you created um then of course you will have your site with the same thing your domain and then over here it says name it's the name of the actual site um like i'll show you an example just so for those who may not be familiar uh let's see so i have as you can tell i have my goat.net which is my domain sharepoint.com sites and then the levelment so the levelmate is my actual site you can have many sites you know i call this the levelmix i use this site for testing just like what i'm doing now with these videos right so i can show you um and then of course you have your doc library so again let me kind of go back you could tell it's a shared document then whatever that document is if you're if you go to sharepoint you click docs you can kind of tell on the you there on the url see where it says shared doc documents and that's kind of where this starts shared documents and then you have the actual name of the documents in this case i it for me it's called youtube you're maybe called something else right so that's kind of you know it's like the starting point of your doc um on which folder you're gonna be working with and then eventually from there you may dig into subfolders right like i have one it's called 2020 but you may have many more folders in there we'll we'll see how that operates in a minute for me i actually have my config file with um with my actual credentials but i'm gonna have to add that on the side because again i don't wanna you know show my credentials so let me do that right quick all right so now my config is ready the next thing i'm going to do is my sharepoint file so let's go ahead and get everything imported that we need so again you need share pump and you know the package installed on your environment so it will be share plump um in this case it will be import um site and office 365. share plump um sites import the version of which version we're going to be dealing with all right okay so now that i have that piece uh we also need to import in our json and os because again we're we're reading the json file so we need that as well all right so the next thing that we're gonna do is gonna read json file right so that's the first thing we're gonna do let's go ahead and read the file uh you know what i missed a step so before we read the file let's go ahead and set two variables first we're gonna set the the root directory um pass oops directory name os pass and then this would be file the next one would be our config pass um and this will be root oops i missed another forward slash so this will be root and then it would be config.json because again this that's this file over here all right so that gives you my config pass again maybe i did it more complicated than i should you could just hard code in do you pass yourself but i noticed it become the pain of the ass sometimes especially when files move or you know you you move this project into another folder but now it doesn't work because you hard coded it to some other folder so not a big fan of that um so it would be config file config json load config file then it's going to be config config sharepoint because that was our that's our um our first key that we have in our json file you know uh which is sharepoint so um all right so that's it we got that read now we're going to start we're going to assign a few variables as well so the next variable we're going to assign is going to be the username um password oops um sharepoint url sharepoint site sharepoint doc all right um that's going to be config username config password uh config over the url config site config dock library all right again this is all stuff that's coming from our configuration file maybe you want to skip it just hard code it in here by me that's totally fine whatever works for you right um so next thing is we're going to create a class we're going to call this sharepoint and the first message that we're going to call we're going to create on here will be our authentication so next we're going to do us cookies office 365 and we're doing sharepoint url uh username is that it right there it goes it is right username username password password and then i'm gonna be i'm gonna do a get cookies and that's it that will give you my authentication cookies back that's needed to authenticate the next is gonna be connecting to the site so it's site sharepoint site um the version all right so there's different versions in this case right sharepoint i'm dealing with v 365. because again that's office 365 so that's that's what i'm working with so that's the version that i'm dealing with um us cookies us cookies and then we're gonna return site all right so that's this is the first method the second one we're gonna do is called connect folder all right so what we're doing here is now that so whenever we authenticate to sharepoint since we're gonna be downloading a file now we need to specify what folder we're gonna connect to like we don't it doesn't know where we connect them to unless we tell it and that's kind of where the um connect folder comes in like we got to connect to a phone so we authenticate first connect to the folder second then with once we're in that folder then we can specify the actual file that we want to download or you know so on so so again it's friday love having me a whiskey every once in a while so that's my drink of choice today whiskey on the rocks there's not much rocks no more pretty much melted but nevertheless it's good uh let's see what am i doing connect yep i'm going to connect the folder so let's just create a aus site um we're going to call this us method to authenticate all right um so the next thing is we need to build our directory pass so we specified up here on top a sharepoint directory folder that we're gonna that we're focusing on if you're if that's not the case for you you're gonna be bouncing around from folder to folder the different folders then i would recommend this is not needed for you this is something that you would just determine when you pass in your parameter now for me i'm only dealing with one starting folder you know and then of course files and subfolders but if you're dealing with from the highest hierarchy you know of the of sharepoint um directories um you're going to be bouncing around different files folders then yeah this is not needed in your config file you would just specify specify it in here in your you know your folder name you would you know kind of specify for example so like for a folder name it would be something like you know uh let me kind of go back to my um where is it at dude where my sharepoint now we're just showing y'all right now there it goes okay so like right here right this is your your starting point uh let me go back so let's say if i have more folders i'm going to call another one um there's no testing okay so now i've got two folders if you're not gonna if you're gonna be working within one folder you can do what i did if you're gonna be bouncing around different places then that is where don't even specify this ultimately what you're gonna have to do is specify it in your in this folder name is going to be something like in my case it will be you youtube um it will be like yeah youtube i think i call it 2020 and then of course you have any other subfolders beyond that it could be you know june i don't know right but it'll be that's how it would look right you would just have to specify that um if that's the case in my case though i did specify this this starting point up here so really i just have to specify whatever folder is in and in my case i only have one um subfolder which is 2020 then of course i have files in there so let's go ahead and um continue on um so we're going to have to [Music] um build a directory pass damn it why the hell is it doing that man i knew it man it did that earlier and i thought i missed help but that's not the case here so with sharepoint docs then of course the folder name right so just make sure it makes sense sharepoint docs for me it's called youtube then the folder name is 2020. you know again if you don't if that's not the case for you you don't need to do this piece you're fine um then it's going to be folder all site folders sharepoint directory and then we're going to return folder so again we're we have to do on that this is our authentication object we're going to authenticate we're going to use a folder function uh c um share plump and then of course we're specifying the actual folder that we're going to be accessing right like to the lowest point so my case is two folders youtube 2020 are the two folders i'm going to return that object back so the final piece is going to be we're going to create a message that's called download folder and here we're gonna end up taking um let's see the file name and the folder name okay we'll go past passing those two parameters file name and folder name okay and then we're gonna create a object from the connect folder right we're going to call this connect folder we're going to pass in the folder name again this is one of our parameters this takes a parameter and we're actually just passing what we're getting here to here okay um [Music] and then what i'm gonna do here is gonna be i'm gonna call this um you know what let's just do a return we're going to return folder get file uh file name so again just hopefully this makes sense this connect folder when i call this method it's actually authenticating for me right so it's you know it's running this piece of code it's authenticating i'm specifying the directory path that i want to go to the lowest level where whatever my files are going to be at um and then i get back my folder object of whatever folder i'm at once i'm inside my i got my folder object i'm then i'm calling i'm doing a git file function and all i have to do is specify the file name that's it it's going to return me back that file so this is it for sharepoint now the next step here we're gonna have to [Music] go ahead and import this sharepoint file oops yep so we're gonna do sharepoint for now i'm gonna just do this this is gonna be um set file name so let me call this file name and let's take a look at what we actually have right now that we could you know download pretty much so if i go to sharepoint i'm going to youtube 2020. so we have two files we have the sample pdf file and excel file so let's download the sample pdf file right so that's the actual name sample pdf uh the next thing is we're gonna set the folder name um so again if i go back i specified youtube already in my configuration okay so this shared point doc i already kind of specified but again let's say i did not do that then ultimately for me since i'm dealing with youtube 2020 it would be something like you youtube 2020 right that's ultimately what it will be um but because i did specify i don't need to do that i would just call 2020. i already specified the main folder all i'm doing here is specifying the other folder if i had subfolders like the month june you know that's what i would end up doing but in my case i only have the year um so then the next piece is gonna be get file so i'm to call this file uh sharepoint download folder did i call it folder what the hell my bad download file that's what we're doing we're not downloading folders we're downloading files all right so my mistake download file i'm going to specify [Music] file name and then folder name again file name folder name that's what we have over here file name folder name right so now that i have that the next thing is save file okay so i'm going to pass in my object which is my file it's a binary again i'm dealing with a binary here because pdf and excel um if you're dealing with plain text file then you don't need you know just write it but i'm dealing with binary files i'm writing a binaries that's what wb stands for um right oops my bad i'm totally wrong this where it says open file this is actually gonna be the file name oops all right so the reason why is this is this is the actual name the um the open this open function it's actually going to create a file for me based off this name now because i'm downloading a file from sharepoint i want to at least i'm thinking right you want to keep the same name maybe you don't but if you do use the same file name whatever file name that you're specifying to download you're going to use the same file name as well to open because that's where you're going to you know that's what you're going to generate now what i'm going to write is going to be this object right here this file object and then i'm gonna do a um save and that's it man like we could run and then we're done so i'm gonna go ahead and run it what should happen we should see a pdf file get generated we'll see it dropped in so i'm not specifying the file anywhere specifically in the directory in your case you can in my case you're just going to load it under the same project but um yeah let's go see so oops i'm going to run python project damn it got an error message so what happened here oh you know what i think i know what it's not username i believe it's user yup that's what it is user yup okay so that was my mistake [Music] oops oops my bad i got it wrong again so it's not safe it's already right in it it's closed so you could tell i did write it already over here and i'll probably open it um [Music] let me just go to the folder so here we go right here right here's my folder where i'm working out of and here the pdf document that is open so let's run it again because i know it aired out again this should have been closed not saved because i mean technically i mean it is it's not really saving the file what it's doing it's actually just creating a file and it's writing to it it's not like i'm modified and then i'm going to save it so that's kind of more so my mistake but let me just go ahead and delete and let's run it again and now we should see there you go with no air everything worked fine so again it's not it's really not that hard when you think about it again i kind of just broke it down in samples ideally if you're going to make this more programmatic probably because you have a list maybe you have some kind of excel sheet or maybe you have just a list of files or i mean that's where you start adding some additional logic to be able to kind of go through and download every file or download specific files or or whatever a good scenario for this would be if there's data that's being saved down in sharepoint and you know people are adding data to it because it's on sharepoint you got multiple people sharing it but maybe you need it for your analysis when you're running some kind of monthly report well you know the name of it and even if it changes by years you could make it dynamic you know for every year or even by months you know it's crawled into the right folder and boom grabs the fought right file you know the file name and then there you go once you download it save it locally you could start extracting some data you know do whatever do whatever you want again there are so many different reasons to do it but again um this was a request from somebody i really hope that you see the video i'm hoping that it helps you out if it does again give me a thumbs up um you know i'm working on building my my channel kind of talked about a little bit trying to figure out like what's the best approach to kind of tackle my channel and i think for you know there's a few languages that i know but i think i'm gonna stick with python man i like python a lot it's my it is my main language and i think i could bring some value and help you all out so you have any ideas you want me to make a video or something in python send me a message man you know leave a comment and um just like somebody asked requested for this video i may make a video over your request as well and um man whatever i can do to help you all out man again i'm trying to you know my main purpose here is to kind of help you all out if i can anyway so all i ask to return man if you kind of just show some love back hit that thumbs up follow me and um you know hopefully we can help each other out man um appreciate it for watching um thanks for the love and the support again um leave your comments man let me know what you want and i'll put effort to make a video peace
Info
Channel: I am Lu
Views: 32,731
Rating: undefined out of 5
Keywords:
Id: TNaObTGJMOc
Channel Id: undefined
Length: 29min 35sec (1775 seconds)
Published: Sat Jul 10 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.