Created
March 25, 2014 16:01
-
-
Save hur1can3/9764998 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
| #!/usr/bin/env python | |
| # filename: shrinkpdf | |
| # description: A Python wrapper for the gs command, to reduce the filesize of PDFs | |
| # fileversion: 0.2.1 | |
| # date: 20130506 | |
| # author: Dayo Adewunmi, Matthew Levandowski | |
| # email: jargonsummary@gmail.com | |
| # license: GPLv3 | |
| # python version: 2.7.3 | |
| from optparse import OptionParser | |
| import os | |
| import subprocess | |
| def pdfexists(origpdf): | |
| """ Check if there are any PDF files in the cwd. """ | |
| if os.path.isfile(origpdf): | |
| splitOrigFilename = os.path.splitext(origpdf) | |
| if splitOrigFilename[1].lower() == ".pdf": | |
| return True | |
| else: | |
| return False | |
| def shrunkpdfexists(shrunkpdf): | |
| """ Check if this is already a shrunk PDF file. """ | |
| if shrunkpdf[:5] == "spdf_": | |
| return True | |
| else: | |
| return False | |
| def searchDir(sourcedir): | |
| """ List all files and directories in the cwd. """ | |
| for filedir in os.listdir(sourcedir): | |
| if pdfexists(filedir): | |
| if shrunkpdfexists(filedir): | |
| return False | |
| else: | |
| shrinkpdf(filedir) | |
| def shrinkpdf(origpdf): | |
| """Use subprocess.call() to call gs and shrink the PDF file, saving with 'outputfile' | |
| as filename for newly shrunk PDF.""" | |
| shrunkpdfname = 'spdf_' + origpdf | |
| # Added windows 64 bit ghostscript compatibility | |
| subprocess.call(["gswin64c", "-dNOPAUSE", "-dBATCH", "-sDEVICE=pdfwrite", | |
| "-dCompatibilityLevel=1.4", "-dPDFSETTINGS=/screen", "-sOutputFile=" + shrunkpdfname, origpdf]) | |
| def main(): | |
| searchDir(os.getcwd()) | |
| if __name__ == "__main__": | |
| main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment