In this post we will be creating a Python script that will compress an existing PDF file, to do this we will be using the PyPDF2 library. The PyPDF2 is a Python library that enables users to perform various operations on PDF documents. It provides a range of functions for reading, editing, and manipulating PDF files, such as merging and splitting PDFs, extracting text and images, and encrypting and decrypting content.
See the sample of Python code below where we utilize the PyPDF2 module to compress a PDF file, we start by initiating our PDF reader to read from our existing PDF. We then move on to writing its content to a new PDF using the zlib/deflate compression method. Note that this is a lossless compression, meaning the resulting PDF should look exactly the same.
Some may also view this process as CPU intensive although for single PDF use this will suffice.
from PyPDF2 import PdfReader,PdfWriter
reader = PdfReader("file.pdf")
writer = PdfWriter()
for page in reader.pages:
page.compress_content_streams()
writer.add_page(page)
with open("compressed_file.pdf", "wb") as f:
writer.write(f)
An example of a PDF prior to the use of the above can be seen below.(61.2KB)

See below the same after the script run.(40.8KB)

Take a look at some of our other content around the Python programming language by clicking here.