Grab Cell Ranges From Excel With Python - Python and Excel With OpenPyXL #7

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
okay in the last video we looked at grabbing whole columns and whole rows in this video i want to show you how to grab a range any specific range that you want so i came down and commented out the stuff from the last video so here let's grab a range so to do this i'm going to call this range and it's just like before we grab the ws the worksheet and inside of here we just set up a slice that we want so any slice that we want and we use a colon for that so let's go for instance a2 to a10 and if we look at our spreadsheet that's just going to be a2 to a10 so john through steve that's a slice right slices in python are always used with colons usually so okay we can grab that now let's print this out just to see what this is so print range let's go ahead and save this head back over here and let's run this guy let me clear the screen and here we see this is definitely a tuple but it looks like inside of our tuple there are other tuples so this is a tuple full of tuples which means we're going to have to loop through this a couple of times in order to get this data out so we can do that we learned how to loop in the last video so let's go loop so let's go for cell in range and again i just invented this cell you could call this anything you want you could call it x if you wanted to it doesn't really matter so instead of here we can print out cell now if we save this and run it you'll notice there are two brackets there are two parentheses right the first time if we run this again now well hold on we're still printing out this let's get rid of this save this now let's run this again now it's just printing out each individual tuple right we still need to loop through here again to get the stuff that's inside of each of these tuples right so we can do that again let's come down here and instead of printing this out let's do another for loop inside of this loop and you'll notice here i'm in indenting i'm using the tab key on my keyboard do not use the spacebar to do it like that you have to tab these things python is tab sensitive so okay so inside of here we can do it again so i'm going to call it x this time for x in cell which is this variable here we want to print out x so if we save this and run it and let's clear the screen we get the same thing but now the tuple parentheses are gone from each one which means we can dot value this now right so here we can dot value save this run it one more time and we get the values john through steve a2 through a10 which is exactly this a2 3 a10 so if you have a specific slice you want to get this is how you can do that so you can get crazy with this we can go a2 through a2 through b10 for instance if we want all of the data save this and run it let's go ahead and clear the screen now we get john blue aaron red sam pink tina green josh yellow etc all the way through here because we're grabbing all of it one big crazy slice so those are slices those are ranges maybe not quite as useful but definitely a tool to keep in mind if you have a very specific range of stuff you want to get and that's all there is to it so in the next video we'll start to look at iterating through columns and rows specifically and that'll be coming up
Info
Channel: Codemy.com
Views: 37,954
Rating: undefined out of 5
Keywords: Python Excel OpenpyExcel Ranges, python excel openpyxl, grab ranges from spreadsheet with python, ranges of cells with python, ranges of cells in excel with openpyxl, openpyxl ranges, openpyxl cell ranges, cell ranges with openpyxl, codemy.com, john elder, python openpyxl tutorial #7, cell ranges with excel and openpyxl, grab a range openpyxl, openpyxl excel spreadsheet ranges
Id: vDbXToL9xdM
Channel Id: undefined
Length: 3min 54sec (234 seconds)
Published: Fri Apr 16 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.