1
00:00:00,510 --> 00:00:07,230
And this video, we're going to load a PDF file in Python and extract its text.

2
00:00:07,590 --> 00:00:12,370
So we're going to get the text as a Python string to do this.

3
00:00:12,390 --> 00:00:21,360
We're going to use the P Y M U PDF library that one.

4
00:00:23,060 --> 00:00:28,550
Once that's a library is is told, you imported as import fits.

5
00:00:30,060 --> 00:00:37,740
So that is p y you the f then you say with its that's open.

6
00:00:38,070 --> 00:00:40,080
So we use a with context manager.

7
00:00:41,790 --> 00:00:50,550
Students, that PDF, this is a file that I have fear in my report, you can get this by following this

8
00:00:50,550 --> 00:00:51,090
report.

9
00:00:51,240 --> 00:00:53,490
The URL is here, as always.

10
00:00:54,120 --> 00:00:57,030
And these girls as file.

11
00:00:58,140 --> 00:01:00,810
So we create this file object.

12
00:01:00,900 --> 00:01:10,770
Using that file path, you decides what you want to do for each page of that file object.

13
00:01:11,100 --> 00:01:19,470
So for page in file, or if you want to give this a more meaningful name, you could see as PDF full

14
00:01:19,470 --> 00:01:22,920
page in PDF that makes more sense.

15
00:01:23,820 --> 00:01:33,570
You want to print the page, get texts, methods you call, get texts and I'm running this.

16
00:01:34,350 --> 00:01:37,050
It's probably installed, but I did a typo here.

17
00:01:37,710 --> 00:01:38,730
So fits.

18
00:01:39,330 --> 00:01:39,570
Yeah.

19
00:01:40,480 --> 00:01:44,710
Important run, and we got some hot foods.

20
00:01:46,320 --> 00:01:55,530
So these are all the pages, if I open students, those PDF and we can cross-check.

21
00:01:56,340 --> 00:01:57,780
Yeah, it's saucier.

22
00:01:58,080 --> 00:02:04,270
So students, app ideas, general student recommendations and so on.

23
00:02:04,290 --> 00:02:11,790
So as you can see, of course, we cannot have the phones also in our Python stream because the Python

24
00:02:11,790 --> 00:02:18,180
string is just a plain string is just text, so the font will get lost.

25
00:02:18,450 --> 00:02:21,720
The formatting will get lost, and that's a downside.

26
00:02:22,140 --> 00:02:27,420
But again, we are extracting texts, information not style here.

27
00:02:28,110 --> 00:02:35,700
Now, of course, you can do different things with this text now, for example, if you want to just

28
00:02:35,910 --> 00:02:39,930
separate the pages in the council here.

29
00:02:40,170 --> 00:02:48,840
You could just see 20 times this character to make 20 instances of vets.

30
00:02:50,390 --> 00:02:54,110
Dash, just a visibly see no.

31
00:02:54,830 --> 00:02:55,090
Yeah.

32
00:02:55,520 --> 00:03:06,320
So we first print those characters 20 of them, then the contents of a page and then another set of

33
00:03:06,320 --> 00:03:07,220
those characters.

34
00:03:07,670 --> 00:03:11,360
Then this is a second page, as you see here.

35
00:03:11,420 --> 00:03:14,810
We have multiple pages, for example, for example.

36
00:03:15,280 --> 00:03:16,160
And so one.

37
00:03:17,490 --> 00:03:21,600
You can also decide to print only the first page.

38
00:03:22,800 --> 00:03:25,050
You could do PDF zero.

39
00:03:25,290 --> 00:03:27,150
So that is the first page.

40
00:03:27,660 --> 00:03:29,400
Get text.

41
00:03:30,780 --> 00:03:34,380
So Page one is equal to that's a new print.

42
00:03:34,950 --> 00:03:42,360
Page one, so that is only one, you need only one particular page so that to give you the contents

43
00:03:42,360 --> 00:03:43,860
of the first page only.

44
00:03:47,190 --> 00:03:52,370
In other words, we stored the text of a first page in this page one string.

45
00:03:52,950 --> 00:03:59,580
If you want to have one single variable holding all the text of all the pages again, you'd go back

46
00:03:59,580 --> 00:04:00,810
to the for loop.

47
00:04:04,770 --> 00:04:14,850
So here and then full page in PDF, first you create an empty string, texts you consume, that's empty

48
00:04:14,850 --> 00:04:15,210
string.

49
00:04:15,330 --> 00:04:17,070
And then we remove that.

50
00:04:19,290 --> 00:04:29,770
And instead of printing, we see text a sequel to that plus texts actually text plus VAT.

51
00:04:30,240 --> 00:04:32,610
So first we start with an empty string.

52
00:04:32,850 --> 00:04:35,520
We add that to the empty string.

53
00:04:36,150 --> 00:04:40,320
So in the first loop, we're going to get the first page only.

54
00:04:41,040 --> 00:04:46,920
And then on top of that first page contents, we add the next page contents.

55
00:04:46,920 --> 00:04:55,410
So we concatenate all the strings and then you can do whatever you want with the final variable text.

56
00:04:56,490 --> 00:04:56,850
Run.

57
00:04:58,680 --> 00:05:02,310
Yeah, we get basically the same old woods with multiple pages.

58
00:05:03,160 --> 00:05:06,120
And that is how you extract text from a PDF.

59
00:05:07,510 --> 00:05:12,800
In the next video, we're going to do some more advanced operations with PDFs, so see you.

