Let's talk about PDFs.
How do we process them?
I mean, we've learned about working with files in our previous section with Python, and you can imagine
that we can work with any files we've learned about image processing, but we've also learned that we
can just manipulate any text file, any PDF file, any CSV or Excel file.
These are all just file types that we can process and work with in Python.
In this section, we're going to talk about PDF and how this might work, and this knowledge of PDF
will transfer to different file formats.
Now we're going to talk about Excel files and CSV files later on in the course when we get into machine
learning.
But for now, let's work with PDFs and see how we can use Python to make our lives easier.
Now I'm going to link to some PDF files that we have here that we're going to use in this section.
We have a simple dummy PDF file.
We have another PDF file that actually has two pages.
And then we have a watermark PDF which we're going to save for our final project in this section.
But let's go back here.
From what we've learned so far, what do you think our first step is?
Well, our first step is to find a library that we can use with Python to process PDF files.
And get ready for this section.
I have a really impressive website, probably the best website you've seen, probably the cleanest,
most modern website.
When it comes to the package that we're installing, we're installing something called PIP three install.
P.
Y.
PDF.
Two.
Now there's a lot of packages that we can install when it comes to PDFs.
This is the one that I recommend for the course.
Now, while this is installing, in my case, it's already installed.
Let me show you what their website documentation looks like.
Get ready.
It's pretty.
Here it is.
Wow.
Right.
I don't know why, but for some reason, python documentation websites are not the prettiest.
I mean, this is all subjective, right?
Some people like this, but it just doesn't get you excited about working with PDFs.
This website does it?
Anyway, you can see over here that it's actually not that big of a library.
And you can click on the documentation and again read through everything.
But let's just learn the basics.
So the first thing I'm going to do is import pi pdf to.
And what we're going to do is work with the first one, the dummy PDF, which is just this PDF file
that is one single page that says dummy PDF file.
Nothing too exciting.
Now.
How can we open this file?
We know this from previous lessons, right?
We can use that with open command and we'll say dummy dot pdf and I can say it like this because we're
in the same folder.
I have a PDF file that I've created, so I'm just saying hey, grab the dummy pdf from the same directory.
And in here let's for now just do read because I want to read the file and then just say as file and
for now print file to see what we get.
I'm going to run this file by saying Python three pdf dot pie.
Let's see what happens.
All right.
I got a text.
Oh, rapper name, dummy PDF.
All right, it looks like it's working.
Let's just explore this a little bit more to see that it is indeed working.
What we can do is actually use the PDF to read the file.
So.
Let's do this.
We can say reader equals pie pdf and use the method pdf.
File reader.
Again.
It allows us to read this Python file.
So let's just give it file in here.
And then we'll just print reader and we can ask for a number of pages.
Again, this comes with the library, so let's run this and see what happens.
All right, we get an error.
It says pdf read warning PDF File Reader stream file object is not in binary mode.
What does that mean?
Well, remember, binary is ones and zeros.
Right.
If I click on Dummies.
Dummied up pdf.
This is not the PDF we thought this is how a pdf should look.
But you see, depending on the program that you're running, it might not be able to read the PDF file.
So what we need to do, and this is just a standard that you have to remember, is that we want to read
the binary.
So it's read and then binary.
So what happens here is that this creates a file stream object.
So it's going to convert the file object to binary mode.
So that the pdf to file reader.
Can read this binary file object.
So now if I run this again, look at that.
It's able to read it.
And it tells me that this has one page.
If I do two page dot pdf and run this.
We get two pages.
Awesome.
Let's clear this.
And go back to our pdf.
I feel bad for naming this pdf dummy.
I shouldn't be mean to this pdf.
I'll I'll work on my naming conventions next time.
So just like we saw in the image processing with pi pdf, we can do a lot of things to process this
image.
For example, I can do get page here.
And if I do get page, let's say the first page and I run this.
We get the page object over here.
Now it looks like gibberish here because, well, this is a special object that a PDF creates.
But if I do something like page one, which will be the page two in this case.
I get an error, an index error saying list index out of range because the dummy pdf only has one page.
We can also do different things like let's for example, rotate this image and we'll rotate it 180 degrees.
If I run this.
This doesn't work.
Now why is this?
It's because p pdf two uses the reader object, but it needs to know what to rotate.
In our case, we actually have to get the page of the reader to rotate it.
So we have to do something like reader dot.
Get page.
Get the first page of our dummy pdf.
And assign this to the page variable so that now that we have the page object, remember a page object
and a reader object.
I now run this well I get page object has no attribute rotate and I may have tricked you a little bit
to prove a point here.
Now, this rotate object sounds like something that it should have, but it's actually not the right
name that we want.
So how can we solve this?
And this gets back to us reading the documentation.
If I go back to the file reader and let's search, rotate or let's go back here, we see that we have
the page object class, which we have, right?
We have the page object.
If I click over here, let's find rotate.
And we have a few things we have merge rotate page.
We keep going, keep going, keep going and all right, we have rotate clockwise, rotate counterclockwise
and the angle.
So it looks like this is what we need.
We don't actually have a rotate method, but you see here, this is how we figure out what the library
does.
We also have scale to scale by if we want it to.
So let's just copy rotate clockwise or let's do rotate counterclockwise.
And we'll just do 90.
If I save this and run.
All right, we get back an object here, but this object is actually in memory.
If I go back to my dummy.
Nothing has changed because we haven't really saved anything.
This is all in my computer's memory.
When this program stops running, well, it gets trashed.
So let's update our dummied up PDF to actually write and rotate counterclockwise.
How can we do that?
Well, with another with keyword.
So inside of with open we're going to say with open and we'll call this, let's say crooked.
I don't know if this is how you spell cricket, so let's go with tilt dot pdf.
And we're going to open this file and we're going to write to this file and binary format.
And we're going to say as file.
And in here, we're going to write the file to this new file.
But how can we do this?
This library gives us something called a writer object so we can create a writer variable.
And this writer variable will use the.
PDF dot pdf instead of file reader.
We have a file.
Writer.
And this allows us to write the object and memory so that now I can say writer.
Dot.
Right.
And just to make the naming convention appropriate, let's say this is going to be the new file.
So we'll say new file.
So let's see if this works.
I'm going to clear.
And let's run this.
All right.
We have tilt at PDF.
But we actually forgot to do one thing because we've just opened a writer object, but we're not actually
writing any PDFs into it.
Ideally, we get the rotated page and write inside of it, right?
So what we can do?
Is remove the print here.
We've rotated the page and with the writer object that we have now, we can say writer, dot, add page
and give it the page that we've rotated.
So now if I run this one more time.
And go to tilt.
Well, again, we see gibberish here.
But if we open this.
Look at that.
Our pdf is rotated just like that.
Very, very cool.
So that's great, but kind of useless, right?
Who really rotates PDFs?
That's really hard to read, but at least this shows you the ability for us to process PDFs.
Let's work on something more exciting in the next video.
I want us to do something a little bit more useful than this.
So let's take a break and I'll see you in the next one.