How do I extract the text of a single page with PyPDF2?

Question:

I have a document library which consists of several hundred PDF Documents. I am attempting to export the first page of each PDF document. Below is my script which extracts the page. It saves each page as an individual PDF. However, the files which are exported seem to be exporting in unreadable or damaged format.

Is there something missing from my script?

import os
from PyPDF2 import PdfReader, PdfWriter

# get the file names in the directory
input_directory = "Fund_Docs_Sample"
entries = os.listdir(input_directory)
output_directory = "First Pages"
outputs = os.listdir(output_directory)

for output_file_name in entries:
    reader = PdfReader(input_directory + "/" + output_file_name)
    page = reader.pages[0]
    first_page = "n" + page.extract_text() + "n"

    with open(output_file_name, "wb") as outputStream:
        pdf_writer = PdfWriter(output_file_name + first_page)

enter image description here

Asked By: Data_Science_Mick

||

Answers:

I think the cause coul be the fact that in your code is missing the method call addPage(page) in whitch you specify the contents of the first page of the output file. The code that you need is similar to the one proposed in the answer to this question. In particular, a possible solution could look like this:

import os
from PyPDF2 import PdfReader, PdfWriter

# get the file names in the directory
input_directory = 'Fund_Docs_Sample'
entries = os.listdir(input_directory)
output_directory = 'First Pages'

for entry in entries:
    # create a PDF reader object
    with open(f"{input_directory}/{entry}", 'rb') as infile:
        reader = PdfReader(infile)
        writer = PdfWriter()
        writer.addPage(reader.getPage(0))

        with open(f"{output_directory}/{entry}", 'wb') as outfile:
            writer.write(outfile)

With this code, the names of the PDFs with only the first page will be the same of the corresponding original PDF but they will be located in the output_directory

Answered By: trolloldem
  1. You’re missing pdf_writer.write(outputStream)
  2. Do you want to write a text file (containing the extracted text) or a PDF file (containing the first page of the PDF)?
  3. You seem to overwrite the files of the input
  4. output_directory is not used at all

After reading the comments, you likely want this:

from pathlib import Path
from PyPDF2 import PdfReader

# get the file names in the directory
input_directory = Path("Fund_Docs_Sample")
output_directory = Path("First Pages")

for input_file_path in input_directory.glob("*.pdf"):
    print(input_file_path)
    reader = PdfReader(input_file_path)
    page = reader.pages[0]
    first_page_text = "n" + page.extract_text() + "n"
    
    # create the output text file path
    output_file_path = output_directory / f"{input_file_path.name}.txt"
    
    # write the text to the output file
    with open(output_file_path, "w") as output_file:
        output_file.write(first_page_text)
Answered By: Martin Thoma