 Welcome, I'm Dan. And in this video, I want to show you how to use Python to take a multi-page PDF and crop every page in that PDF and resave it. You don't have to go out and buy expensive software or any sort of specialty proprietary software, you can just use Python, which is built into a lot of operating systems. So let's go ahead and get started. The first thing you're going to need to do is install an external package. There are a few that can do this. The one I'm going to be using is called PyPDF2. You can do a pip install PyPDF2. It run that from your terminal, and that'll install the necessary packages for you. Here I'm not going to run that in this case, since I already have it installed. But that's that's how you get it installed. First, you're going to want to import the class from that PyPDF2 module. We're going to import two classes, actually, the PDF reader and the PDF writer. That's all you're going to need at this point. So we'll go ahead and run that. The first thing we're going to want to do is create a reader object. So let me just take a let me just show you what PDF I want to crop here. You can see that I have this PDF called carols. It's a bunch of sheet music for Christmas caroling. And you can see every page has this ugly black black edge on it. And I just want to crop that out of there. So I'm going to go ahead and take that carols PDF. So the way to do this is we're going to call this object reader. And we're going to do a make a PDF file reader object by passing it the name of the PDF, excuse me, .pdf. I'm going to put in read mode. And we'll go ahead and do that. And that's that's pretty quick. So there are a couple things you can take a peek at for one, you can see how many pages this particular PDF has. So to do that, you do a get number of pages. And hit enter, you can see that I've got 57 pages that I want to crop. If I were to do this manually, that would take quite a while. So it's nice that there are some tools out here to allow us to do that programmatically. So just as an example, we're just going to extract a single page from this PDF to sort of get some dimensions. In this particular PDF, every page is the same size. If I recall correctly, it's legal size. So it's 14 inches wide by eight and a half inches tall, it's legal landscape. So I'm going to just create a page. And I'm going to set that equal to reader dot get get page. And we'll just get the fifth page in this case, or the sixth page since we're kind of starting from zero. So go ahead and hit that. And so we have this page object. So now I just want to, what we're going to need to do to be able to crop is we need to take a look at the basically the XY coordinates where we want to actually make those crops. So here we can use the crop box method, we'll do a get lower left, we'll print that out, get the upper left, get the upper right and the lower right. And it returns four tuples. And basically what you can see those numbers don't make a lot of sense. I told you it was a 14 inch by 11 by eight and a half inch page. So what's this 612 and 1008? And I'll show you here, basically the default for this particular PDF the default units were in point or PT. And the way we can test that we know that one point one PT is equal to 172nd of an inch. So if I do 14 inches and multiply that time 72, you can see I get 1008 so you can so you know that we're 1008 points wide or 14 inches wide. So that's where that number comes from. So basically, in order to figure out how we want to crop this, let me just draw a quick diagram for you to show you what this will look like. So if this is blue here, if this is your, this is the PDF page, imagine back to geometry and, you know, drawing things on an XY axis. Here's your x axis. And here's your y axis. And so each corner of this blue box is represented by coordinate pair. So down here at your origin, you're going to be zero zero. So your x is zero and your y is zero up here, your x is going to be zero and your y will be the value of y at the top left upper left at the upper right, you're going to be the value of x and the value of y. And at the bottom right, you're going to be the value of x and zero as y. And so that's kind of how that works. And that's the set of tuples that were returned to us. So in this case, all I really want to do is I sort of measured this earlier. And I kind of want to cut off just 2.5, 2.5 inches or so, from that left side of the page, like you saw. So I'm going to multiply that 2.5 times 72 to see how many points we need to remove from the x size. So essentially, we'll go back to my diagram here. I'm going to want a sub box that sort of looks like this. So that's that's my that's the size of the the crop extents that I want. So essentially, this is going to be 800. Excuse me, this will be 180. For x, it'll be zero for y. And then in this case, all we're going to need is the lower left and upper right. So up here, it's not going to change from those default values that we had. So it's going to be 1008 and 612. So we'll do 1008 for the x and 612 for the y. That is some sloppy handwriting. Anyways, so what now what we'll do is before we can create a new PDF, we need to instantiate a writer object. So we'll just call this PDF. We'll just call this writer and call the PDF writer class. And now we're going to make a do loop. So we're our for loop, excuse me. So we're going to essentially loop through every page in the carols.pdf that reader object. And and then crop the page and then add it to this new writer object that we just instantiated. So this is going to be really easy for I in range. And the range is going to be r dot get get number, excuse me, for I and reader dot get number of pages. I'm going to set page equal to reader. And the reader has a method get get page. And we're going to feed that I. So for each loop, we're going to get that page. And we're going to set the crop box. We're going to do the the setter method set lower left at 180 zero and page dot crop box dot set upper right. And we'll do that at 1008 and 612 and and basically what you need to feed to the setter methods is a tuple of your XY pair. And once that page has been essentially cropped using this crop box method, we'll take our writer object and we'll add that page. So we'll add page, excuse me, that has to be a capital P, add page and add that page in. And we'll go ahead and run that and you can see that it doesn't take any time at all even for 57 pages worth of PDFs. Now to write to a file we need to the writer object has a write method, but it takes a stream object type as the input. So essentially what we're going to need to do is we're going to need to define an out stream. So I'll call that out stream. We'll set it equal to we'll use the the key argument open. And we're going to just call this new one carols cropped because that's what it is. It's some Christmas carols that are cropped. In Python three, we're going to you need to specify that it's bytes so we'll specify writes and then bytes. So we set that we now we're going to take that writer object use the write method, and then feed it the out stream. And then lastly, we just want to make sure to close out that out stream when we're done with it to avoid any sort of memory errors or issues with exceptions with that. So we'll go ahead and run that. And that just takes an instant. And so here I'll go ahead and open this up carols cropped. And you can see here are the carols and all the pages no longer have much of a black edge on them. So there you have it. That's how you do multi page cropping in Python. I hope you enjoyed this video. And if you did please be sure to hit subscribe and catch more of them. Thanks for joining me