1import sys
2import pyPdf
3
4def extract(in_file, coords, out_file):
5 with open(in_file, 'rb') as infp:
6 reader = pyPdf.PdfFileReader(infp)
7 page = reader.getPage(0)
8 writer = pyPdf.PdfFileWriter()
9 page.mediaBox.lowerLeft = coords[:2]
10 page.mediaBox.upperRight = coords[2:]
11 # you could do the same for page.trimBox and page.cropBox
12 writer.addPage(page)
13 with open(out_file, 'wb') as outfp:
14 writer.write(outfp)
15
16if __name__ == '__main__':
17 in_file = sys.argv[1]
18 coords = [int(i) for i in sys.argv[2:6]]
19 out_file = sys.argv[6]
20
21 extract(in_file, coords, out_file)
22