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.
How to use python to compress pdf file?
We can use pdf_compressor.py, You can download it in here.
This python script use python to call ghostscript to compress pdf file.
How to use pdf_compressor.py 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' }
where
/default
/prepress: 300dpi
/printer: 300dpi
/ebook: 150dpi
/screen: 72dpi
For example:
compress("source.pdf", 'compressed.pdf')
Hi,
I have saved the code to a directory with a file pdf_compressor.py 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 ?
You should import pdf_compressor model, then use compress().
import pdf_compressor
pdf_compresser.compress("source.pdf", 'compressed.pdf')
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?
If you want to install and use ghostscript in python, you can view this tutorial:
Fix OSError: Unable to locate Ghostscript on paths for Python Beginners – Python Tutorial