PDF processing related (split, etc.)

PDF division method (example of dividing by 25 pages)

・ The number of pages is stored in pgn ・ Add pages with pdfWriter.addPage (reader.getPage (j))

import PyPDF2
import os
os.chdir(hogehoge)

pdf_name = "hogehoge"
reader = PyPDF2.PdfFileReader(pdf_name)
pgn = reader.getNumPages()

for i in range(0, pgn, 25):
    pdfWriter = PyPDF2.PdfFileWriter()
    for j  in range(i, i+25):
        
        if j >= pgn:
            break
        pdfWriter.addPage(reader.getPage(j))
    pdfWriter.write(open(hogehoge.pdf".format(i), "wb"))

Recommended Posts

PDF processing related (split, etc.)
Split PDF into arbitrary pages