Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Compress PDFs using Python

Tags:

python

So I have a gazillion pdfs in a folder, I want to recursively (using os.path.walk) shrink them. I see that adobe pro has a save as reduced size. Would I be able to use this / how do you suggest I do it otherwise.

Note: Yes, I would like them to stay as pdfs because I find that to be the most commonly used and installed fileviewer.

like image 466
Eiyrioü von Kauyf Avatar asked Apr 26 '12 03:04

Eiyrioü von Kauyf


People also ask

How do I compress the size of a file in Python?

To create your own compressed ZIP files, you must open the ZipFile object in write mode by passing 'w' as the second argument. When you pass a path to the write() method of a ZipFile object, Python will compress the file at that path and add it into the ZIP file.


1 Answers

From the project's GitHub page for pdfsizeopt, which is written in Python:

pdfsizeopt is a program for converting large PDF files to small ones. More specifically, pdfsizeopt is a free, cross-platform command-line application (for Linux, Mac OS X, Windows and Unix) and a collection of best practices to optimize the size of PDF files, with focus on PDFs created from TeX and LaTeX documents. pdfsizeopt is written in Python..."

You can probably easily adapt this to your specific needs.

like image 81
kqw Avatar answered Sep 19 '22 10:09

kqw