Created
December 30, 2021 12:53
-
-
Save ilovefreesw/7eab3ebcb6f4cd69cf275f9a21c7eb48 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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
General command:
gswin64 -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -dNOPAUSE -dBATCH -dQUIET -sOutputFile=Output.pdf Input.pdf