Skip to content

Instantly share code, notes, and snippets.

@twosky2000
Forked from ilovefreesw/pdfcompress.py
Created January 15, 2023 16:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save twosky2000/83832a00547cc52fb652a96494322a37 to your computer and use it in GitHub Desktop.
Save twosky2000/83832a00547cc52fb652a96494322a37 to your computer and use it in GitHub Desktop.
A Python script to batch compress PDF files on Windows, MAC, and Linux. Make sure gs and Python are in PATH before running this script.
from __future__ import print_function
import os
import subprocess
root = "."
try:
os.mkdir('compressed')
except FileExistsError:
pass
for file in os.listdir(root):
if file.endswith(".pdf"):
filename = os.path.join(root, file)
arg1= '-sOutputFile=' + './compressed/' + file
print ("compressing:", file )
p = subprocess.Popen(['gs', '-sDEVICE=pdfwrite', '-dCompatibilityLevel=1.4', '-dPDFSETTINGS=/screen', '-dNOPAUSE', '-dBATCH', '-dQUIET', str(arg1), filename], stdout=subprocess.PIPE).wait()
@twosky2000
Copy link
Author

Initial bookmark

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment