Extract the first page of pdf document in python

Python August 17, 2023 python

To extract the first page of pdf in python install the package PyPDF2

python
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import PyPDF2

def extract_first_page(pdf_path, output_path):
    pdf_reader = PyPDF2.PdfFileReader(open(pdf_path, "rb"))
    if pdf_reader.numPages > 0:
        pdf_writer = PyPDF2.PdfFileWriter()
        pdf_writer.addPage(pdf_reader.getPage(0))

        with open(output_path, "wb") as output_file:
            pdf_writer.write(output_file)