Python script to extract pages from a postscript file

This script will examine a postscript file and output individual pages as separate ps files. This is useful for separating pages for figures.

import subprocess
import sys
import numpy as np
import os

if (len(sys.argv) > 1):
fileName = sys.argv[1]
print fileName
callStr = "-c %%Page: "+fileName
#print 'calling: '+callStr

process = subprocess.Popen(['grep '+callStr],shell=True,stdout=subprocess.PIPE)
pages = process.communicate()
pages = int(pages[0])

print '%i pages to extract' % (pages)
for i in np.arange(1,pages+1):
tempSplit = os.path.splitext(fileName)
tempName = tempSplit[0]+'_page'+str(i)+tempSplit[1]
print 'Outputing file: '+tempName['psselect','-p'+str(i), fileName, tempName])