pythonpdf

split a multi-page pdf file into multiple pdf files with python?


I would like to take a multi-page pdf file and create separate pdf files per page.

I have downloaded reportlab and have browsed the documentation, but it seems aimed at pdf generation. I haven't yet seen anything about processing PDF files themselves.

Is there an easy way to do this in python?


Solution

  • Updated solution for the latest release of PyPDF (3.0.0) and to split a range of pages.

    from PyPDF2 import PdfReader, PdfWriter
    
    file_name = r'c:\temp\junk.pdf'
    pages = (121, 130)
    
    reader = PdfReader(file_name)
    writer = PdfWriter()
    page_range = range(pages[0], pages[1] + 1)
    
    for page_num, page in enumerate(reader.pages, 1):
        if page_num in page_range:
            writer.add_page(page)
    
    with open(f'{file_name}_page_{pages[0]}-{pages[1]}.pdf', 'wb') as out:
        writer.write(out)