Compress a PDF using Python for FREE

V Anirudh Sharma
1 min readJun 14, 2021

Other than the simple and comprehensible syntax, what makes python powerful is the libraries from the community.

This is a short article on a simple trick to compress PDFs by 3 folds in size for free using one these Python libraries.

The Library

Let’s look at PDFNetPython3 , the library of interest, by user mnguyenpdftron. Check this for the project description of this library,

The code here has been written to be executed on Google Colab. Readers can choose any method of uploading the file.

THE PROCEDURE

Step 1: Installing PDFNetPython3

pip install PDFNetPython3

Step 2: Importing libraries and tools

Line 4 is strictly for colab users.

Step 3: Upload the PDF

Again, this step executed this particular way is for Colab users only

Step 4: Compress the PDF

We open the PDFDoc object, optimize it and finally save the modified PDF, and close the object.

--

--

V Anirudh Sharma

Engineering undergraduate student. Reader of non-fiction books. Poetry and art for myself.