As I have mentioned before, I volunteer at a Not-For-Profit organization to take care of their IT needs and also take care of the bookkeeping. Every month, our accountant would send me a PDF file that contains the paystub of all our staffs – and everyone is in a single file! Like many other company/organization, we have moved from in-person to mostly remote. I need to email the paystubs to our staffs when I send them their salary.
I did not want to print to PDF and save each page as a separate file manually, and Python is here for the rescue.
Before you can run this script, you will need to do a pip install PyPDF2 first.
from PyPDF2 import PdfFileWriter, PdfFileReader
inputfile = PdfFileReader(open("w2.pdf","rb"))
for i in range(inputfile.numPages):
output = PdfFileWriter()
output.addPage(inputfile.getPage(i))
with open("document-page%s.pdf" % i, "wb") as outputStream:
output.write(outputStream)
This will loop through each page of the PDF file and then create and write the page content into a new PDF file with the name “document-pages[].pdf” with the [] being an incremental number.