Python Compress PDF File: A Beginner Guide – Python Tutorial

By | August 29, 2020

In this tutorial, we will introduce how to compress a pdf file in python. The best way to compress a pdf file is use other application to compress pdf file using python. For example: Use python call ghostscript.

Python Compress PDF File

How to use python to compress pdf file?

We can use, You can download it in here.

This python script use python to call ghostscript to compress pdf file.

How to use to compress pdf file?

You can use compress() function to compress.

power can be:

quality = {
        0: '/default',
        1: '/prepress',
        2: '/printer',
        3: '/ebook',
        4: '/screen'


/prepress: 300dpi
/printer: 300dpi
/ebook: 150dpi
/screen: 72dpi

For example:

compress("source.pdf", 'compressed.pdf')

4 thoughts on “Python Compress PDF File: A Beginner Guide – Python Tutorial

  1. Siraj Ahmed


    I have saved the code to a directory with a file now in the same directory I have a pdf file which I need to compress say abc.pdf I am trying to run the module/file from IDLE but it throws me some error. Can someone please guide me how to use this file to execute ?

    1. admin Post author

      You should import pdf_compressor model, then use compress().

      import pdf_compressor
      pdf_compresser.compress("source.pdf", 'compressed.pdf')

  2. Umidjon Sadatov

    When I use compress() function, FileNotFoundError appears athough the files exists. How I can solve it? I prepare it is because Ghoscript is not installed in PC, how to install it in Windows10?


Leave a Reply

Your email address will not be published. Required fields are marked *