Extract the first page of pdf document in python

Python -- Posted on Aug. 17, 2023

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

              
                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)
                  
   
            

Related Posts