Skip to content

Instantly share code, notes, and snippets.

Created December 11, 2015 02:50
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
#!/usr/bin/env python
Author: Adam Pah
Converts a directory of keynote documents to pdfs
Hacky hack hack.
#Standard path imports
from __future__ import division, print_function
import argparse
import os
import glob
import re
#Non-standard imports
#Global directories and variables
def fname_handler(inname):
ninname = re.sub(' ', '\ ', inname)
if '(' in ninname:
ninname = '\('.join(ninname.split('('))
if ')' in ninname:
ninname = '\)'.join(ninname.split(')'))
return ninname
def extract_pdf_filenames(keyfile):
path_prefix = 'tmp/%s.qlpreview/' % keyfile
npath_prefix = fname_handler(path_prefix)
#re.sub(' ', '\ ', path_prefix)
#if '(' in npath_prefix:
#npath_prefix = '\('.join(npath_prefix.split('('))
#if ')' in npath_prefix:
#npath_prefix = '\)'.join(npath_prefix.split(')'))
html_lines = [l.strip() for l in open(path_prefix + 'Preview.html').readlines() if 'img' in l]
file_list = [npath_prefix + l.split('src="')[-1].split('"')[0] for l in html_lines]
return file_list
def main(args):
if != './':
glob_path = + '/*.key'
glob_path = '*.key'
#Start iterating
for keyfile in glob.glob(glob_path):
pdf_file = keyfile.split('.key')[0] + '.pdf'
if os.path.exists('tmp') == False:
#Make a temporary directory to house this crap
#Pull out the jamtown
os.system('qlmanage -p -o tmp/ "%s"' % keyfile)
#Get the proper pdf file listing
pdf_file_list = extract_pdf_filenames(keyfile)
#Join the pdf files together
pdf_file_string = ' '.join(pdf_file_list)
os.system('"/System/Library/Automator/Combine PDF Pages.action/Contents/Resources/" -o %s %s;' % (fname_handler(pdf_file), pdf_file_string))
if __name__ == '__main__':
parser = argparse.ArgumentParser(description="")
args = parser.parse_args()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment