Skip to content

Instantly share code, notes, and snippets.

@ckunte
Created September 26, 2017 16:38
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 ckunte/903c1dea176497861433a2a0afd08572 to your computer and use it in GitHub Desktop.
Save ckunte/903c1dea176497861433a2a0afd08572 to your computer and use it in GitHub Desktop.
When run from a parent folder, this script combines all pdf files contained within subfolders, e.g., this is useful in combining platform-wise layout drawings.
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
'''
2014 ckunte
'''
import os
import platform
platforms = os.walk('.').next()[1]
for platform in platforms:
msg = 'echo "Processing ' + str(platform) + ' ..";'
cdr = 'cd ' + str(platform) + ';'
if platform.system() == 'Darwin':
cmd = '/System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py -o' + str(platform) + '-layout.pdf *.pdf;'
else:
cmd = 'gs -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=' + str(platform) + '-layout.pdf *.pdf;'
os.system(msg + cdr + cmd)
pass
@ckunte
Copy link
Author

ckunte commented Sep 26, 2017

Clean up file and folder names: Be aware that the os.walk() will not pick-up folders with long file names. Folders (and preferably files) with long names will need to be renamed first into short names, which can be done using zmv as follows:

#!/usr/bin/env zsh
autoload -U zmv
# Safety first: Always use -n switch to test changes before commit.

# Clean up file names in subdirectories and remove special characters
zmv -n '(**/)(*)' '$1${2//[^A-Za-z0-9.-]/_}'

# Replace spaces in filenames and folders with a hyphen
zmv -n '* *' '$f:gs/ /-'

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