# PDF File reader

**URL:** <https://discuss.python.org/t/pdf-file-reader/29127>\
**Category:** Python Help\
**Created:** [July 6, 2023, 10:51pm UTC](https://discuss.python.org/t/pdf-file-reader/29127 "2023-07-06T22:51:16Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 6, 2023, 10:51pm UTC](https://discuss.python.org/t/pdf-file-reader/29127/1 "2023-07-06T22:51:16Z")

</div>

I am trying to get python code to read my pdf search for a certain keyword but keep coming up empty could anyone help my out !!??!!

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 6, 2023, 11:01pm UTC](https://discuss.python.org/t/pdf-file-reader/29127/2 "2023-07-06T23:01:27Z")

</div>

What are you using to read the PDF?

PDFs usually compress their contents to reduce their size, so you can’t necessarily just read it as a plain text file.

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 6, 2023, 11:04pm UTC](https://discuss.python.org/t/pdf-file-reader/29127/3 "2023-07-06T23:04:14Z")

</div>

i have tried a few things like pdfminer or PyPDF2  
Is it possible to read a pdf file with Python ??

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 6, 2023, 11:12pm UTC](https://discuss.python.org/t/pdf-file-reader/29127/4 "2023-07-06T23:12:04Z")

</div>

Why are you asking whether it’s possible when you’ve already used pdfminer and PyPDF2?

Surely the question should be why it’s not finding the keyword!

What code did you use when you were trying with, say, PyPDF2?

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [July 6, 2023, 11:35pm UTC](https://discuss.python.org/t/pdf-file-reader/29127/5 "2023-07-06T23:35:47Z")

</div>

Simply set up a file handler, then use the `.extract_text()` method on each page, in a loop; that should do it.

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 6, 2023, 11:50pm UTC](https://discuss.python.org/t/pdf-file-reader/29127/6 "2023-07-06T23:50:26Z")

</div>

well im using chatGBT for my code but it cant answer all my question’s

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 7, 2023, 12:11am UTC](https://discuss.python.org/t/pdf-file-reader/29127/7 "2023-07-07T00:11:13Z")

</div>

i tried that i come up empty

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 7, 2023, 12:15am UTC](https://discuss.python.org/t/pdf-file-reader/29127/8 "2023-07-07T00:15:57Z")

</div>

Although ChatGPT can give you code, it won’t necessarily be correct. Sometimes it won’t even run!

Without any code or the PDF, there’s no way to know what’s wrong.

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 7, 2023, 12:17am UTC](https://discuss.python.org/t/pdf-file-reader/29127/9 "2023-07-07T00:17:08Z")

</div>

import PyPDF2  
import re

def extract\_mix\_design\_number(pdf\_path, x1, y1, x2, y2, desired\_mix\_design\_number):  
with open(pdf\_path, ‘rb’) as file:  
reader = PyPDF2.PdfReader(file)  
page = reader.pages[0]  
page\_width = float(page.mediaBox.upper\_right[0])  
page\_height = float(page.mediaBox.upper\_right[1])

```
    crop_x1 = x1 * page_width
    crop_x2 = x2 * page_width
    crop_y1 = (1 - y2) * page_height
    crop_y2 = (1 - y1) * page_height

    cropped_page = page.crop((crop_x1, crop_y1, crop_x2, crop_y2))
    text = cropped_page.extract_text()

    # Use regex pattern to find the mix design number
    pattern = r'Mix Design:\s*(\d+)'
    match = re.search(pattern, text)
    if match:
        extracted_mix_design_number = match.group(1)
        if extracted_mix_design_number == desired_mix_design_number:
            return True
        else:
            return False
    else:
        return False

```

# Example usage

pdf\_path = ‘C:/Users/Operator/Desktop/Batch Reports/New Folder/1.pdf’ # Replace with the actual path to your PDF file  
x1 = 0.8 # Replace with the x-coordinate of the top-left corner of the rectangular region  
x2 = 0.9 # Replace with the x-coordinate of the bottom-right corner of the rectangular region  
y1 = 0.1 # Replace with the y-coordinate of the top-left corner of the rectangular region  
y2 = 0.2 # Replace with the y-coordinate of the bottom-right corner of the rectangular region  
desired\_mix\_design\_number = ‘50’ # Replace with the desired mix design number

mix\_design\_found = extract\_mix\_design\_number(pdf\_path, x1, y1, x2, y2, desired\_mix\_design\_number)  
if mix\_design\_found:  
print(“Mix Design Number found in the specified region”)  
else:  
print(“Mix Design Number not found in the specified region”)

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [July 7, 2023, 12:18am UTC](https://discuss.python.org/t/pdf-file-reader/29127/10 "2023-07-07T00:18:52Z")

</div>

I’ll let you have the code that I use, but as you’ve a different use case, you’ll have to adapt.

What this does is to extract the text from each page and save the text in a text file. I know it works for the PDF files that I used it on, but the files I’ve used it on are not protected in any way, so if you’re not seeing any results, then that’ll be why and I don’t have a solution for protected files.

```python
from PyPDF2 import PdfReader

pdf_document = input("file: ")

file_name = pdf_document.split('.')

text_file = f'{file_name[0]}.txt'

with open(pdf_document, "rb") as filehandle, open(text_file, mode='w', encoding='UTF-8') as output:
    pdf = PdfReader(filehandle)
    num_of_pages = len(pdf.pages)
    for page_number in range(num_of_pages):
        page = pdf.pages[page_number]
        print(f"Page: {page_number+1}", file=output)
        print('', file=output)
        print(page.extract_text(), file=output)
        print('', file=output)

```

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 7, 2023, 12:26am UTC](https://discuss.python.org/t/pdf-file-reader/29127/11 "2023-07-07T00:26:51Z")

</div>

im still new to this so bare with me !!!

```auto
from PyPDF2 import PdfReader

pdf_document = 'C:/Users/Operator/Onedrive/Desktop/Batch Reports/New Folder/1.pdf'
file_name = pdf_document.split('.')

text_file = 'C:/Users/Operator/Onedrive/Desktop/Batch Reports/New Folder/1.txt'

with open(pdf_document, "rb") as filehandle, open(text_file, mode='w', encoding='UTF-8') as output:
    pdf = PdfReader(filehandle)
    num_of_pages = len(pdf.pages)
    for page_number in range(num_of_pages):
        page = pdf.pages[page_number]
        print(f"Page: {page_number+1}", file=output)
        print('', file=output)
        print(page.extract_text(), file=output)
        print('', file=output)

```

is this right ??

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 7, 2023, 12:29am UTC](https://discuss.python.org/t/pdf-file-reader/29127/12 "2023-07-07T00:29:43Z")

</div>

To preserve the formatting of the code (and, in the case of error, the traceback), select the code and then click the `</>` button.

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 7, 2023, 12:31am UTC](https://discuss.python.org/t/pdf-file-reader/29127/13 "2023-07-07T00:31:15Z")

</div>

```auto
import PyPDF2
import re

def extract_mix_design_number(pdf_path, x1, y1, x2, y2, desired_mix_design_number):
    with open(pdf_path, 'rb') as file:
        reader = PyPDF2.PdfReader(file)
        page = reader.pages[0]
        page_width = float(page.mediaBox.upper_right[0])
        page_height = float(page.mediaBox.upper_right[1])

        crop_x1 = x1 * page_width
        crop_x2 = x2 * page_width
        crop_y1 = (1 - y2) * page_height
        crop_y2 = (1 - y1) * page_height

        cropped_page = page.crop((crop_x1, crop_y1, crop_x2, crop_y2))
        text = cropped_page.extract_text()

        # Use regex pattern to find the mix design number
        pattern = r'Mix Design:\s*(\d+)'
        match = re.search(pattern, text)
        if match:
            extracted_mix_design_number = match.group(1)
            if extracted_mix_design_number == desired_mix_design_number:
                return True
            else:
                return False
        else:
            return False

# Example usage
pdf_path = 'C:/Users/Operator/Desktop/Batch Reports/New Folder/1.pdf' # Replace with the actual path to your PDF file
x1 = 0.8 # Replace with the x-coordinate of the top-left corner of the rectangular region
x2 = 0.9 # Replace with the x-coordinate of the bottom-right corner of the rectangular region
y1 = 0.1 # Replace with the y-coordinate of the top-left corner of the rectangular region
y2 = 0.2 # Replace with the y-coordinate of the bottom-right corner of the rectangular region
desired_mix_design_number = '50' # Replace with the desired mix design number

mix_design_found = extract_mix_design_number(pdf_path, x1, y1, x2, y2, desired_mix_design_number)
if mix_design_found:
    print("Mix Design Number found in the specified region")
else:
    print("Mix Design Number not found in the specified region")

```

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [July 7, 2023, 12:35am UTC](https://discuss.python.org/t/pdf-file-reader/29127/14 "2023-07-07T00:35:21Z")

</div>

Yeah looks okay to me.

As you’re use hard code file paths, you can drop the `file_name = pdf_document.split('.')`, but it’ll not harm to leave it there; it’s just not doing anything, is all.

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 7, 2023, 1:00am UTC](https://discuss.python.org/t/pdf-file-reader/29127/15 "2023-07-07T01:00:50Z")

</div>

this is all thats in the txt file

Page: 1

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [July 7, 2023, 1:06am UTC](https://discuss.python.org/t/pdf-file-reader/29127/16 "2023-07-07T01:06:54Z")

</div>

Are you sure that the PDF files have text, and not simply images of text? Can you select the text, with the with your mouse, as you would in a regular text document?

---

<div class="post-metadata">

**Author:** ![MRAB](https://avatars.discourse-cdn.com/v4/letter/m/e9c0ed/32.png) [@MRAB](https://discuss.python.org/u/MRAB)\
**Post date:** [July 7, 2023, 1:07am UTC](https://discuss.python.org/t/pdf-file-reader/29127/17 "2023-07-07T01:07:31Z")

</div>

I downloaded PyPDF2 3.0.1 from PyPI and found that `page.mediaBox` is now `page.mediabox` and there’s no `page.crop`.

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 7, 2023, 1:07am UTC](https://discuss.python.org/t/pdf-file-reader/29127/18 "2023-07-07T01:07:59Z")

</div>

how could i show you my pdf ??

---

<div class="post-metadata">

**Author:** ![rob42](https://sea2.discourse-cdn.com/flex002/user_avatar/discuss.python.org/rob42/32/8218_2.png) [@rob42](https://discuss.python.org/u/rob42)\
**Post date:** [July 7, 2023, 1:11am UTC](https://discuss.python.org/t/pdf-file-reader/29127/19 "2023-07-07T01:11:33Z")

</div>

Thank you.

@cadrenw I think that this post was meant to be directed at you.

> [@MRAB](#):
>
> downloaded PyPDF2 3.0.1 from PyPI and found that `page.mediaBox` is now `page.mediabox` and there’s no `page.crop`.

---

<div class="post-metadata">

**Author:** ![cadrenw](https://avatars.discourse-cdn.com/v4/letter/c/ecae2f/32.png) [@cadrenw](https://discuss.python.org/u/cadrenw)\
**Post date:** [July 7, 2023, 1:13am UTC](https://discuss.python.org/t/pdf-file-reader/29127/20 "2023-07-07T01:13:42Z")

</div>

i know about the update thats just the code chatGBT gave me

[Next page](https://discuss.python.org/t/pdf-file-reader/29127.md?page=2)
