Created
June 1, 2015 18:35
-
-
Save mbrgm/b6938a181aacf114494f to your computer and use it in GitHub Desktop.
PDF page cropping
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 | |
from pyPdf.pdf import PdfFileReader, PdfFileWriter, PageObject | |
import glob | |
import os | |
INPUT_DIR = 'input' | |
OUTPUT_DIR = 'output' | |
def main(): | |
input_glob = os.path.normpath(INPUT_DIR + os.sep + '*.pdf') | |
for pdf_file in glob.glob(input_glob): | |
output_pdf = crop(pdf_file) | |
# Construct path of output file | |
filename = os.path.basename(pdf_file) | |
output_path = os.path.join(OUTPUT_DIR, filename) | |
# Write output pdf to file | |
output_stream = file(output_path, 'wb') | |
output_pdf.write(output_stream) | |
output_stream.close() | |
def crop(pdf_file): | |
# Create pdf reader/writer for input/output | |
input = PdfFileReader(file(pdf_file, 'rb')) | |
output = PdfFileWriter() | |
num_pages = input.getNumPages() | |
(width, height) = (217, 161) | |
for pagenum in range(num_pages): | |
current_page = input.getPage(pagenum) | |
if pagenum % 2 == 0: | |
lower_lefts = [ (70, 576), (70, 356), (70, 136) ] | |
else: | |
lower_lefts = [ (68, 561), (68, 341), (68, 121) ] | |
for lower_left in lower_lefts: | |
page = PageObject(current_page.pdf) | |
page.update(current_page) | |
(x_ll, y_ll) = lower_left | |
x_ur = x_ll + width | |
y_ur = y_ll + height | |
upper_right = (x_ur, y_ur) | |
page.trimBox.lowerLeft = lower_left | |
page.trimBox.upperRight = upper_right | |
page.cropBox.lowerLeft = lower_left | |
page.cropBox.upperRight = upper_right | |
output.addPage(page) | |
return output | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment