Untitled

 avatar
unknown
python
4 years ago
1.3 kB
14
Indexable
#!/usr/bin/python3
from PyPDF2 import PdfFileWriter, PdfFileReader
import sys


top_offset = 24
bottom_offset = 6
left_offset = 20
right_offset = 20

if len(sys.argv) < 3:
    print("Usage: pdf-split.py <inputfile> <outputfile>")
    sys.exit(1)

inputfile = sys.argv[1]
outputfile = sys.argv[2]

input1 = PdfFileReader(open(inputfile, 'rb')) 
input2 = PdfFileReader(open(inputfile, 'rb'))
output = PdfFileWriter() 

n = input1.getNumPages()

for i in range(n):
    page = input1.getPage(i)
    heigth = page.mediaBox.getUpperRight_y()
    width = page.mediaBox.getUpperRight_x()
    page.cropBox.lowerLeft = (left_offset, heigth / 2 + bottom_offset)
    page.cropBox.upperLeft = (left_offset, heigth - top_offset)
    page.cropBox.upperRight = (width - right_offset, heigth - top_offset)
    page.cropBox.lowerRight = (width - right_offset, heigth / 2 + bottom_offset)
    output.addPage(page)
    
    page2 = input2.getPage(i)
    page2.cropBox.lowerLeft = (left_offset, top_offset)
    page2.cropBox.upperLeft = (left_offset, heigth / 2 - bottom_offset)
    page2.cropBox.upperRight = (width - right_offset, heigth / 2 - bottom_offset)
    page2.cropBox.lowerRight = (width - right_offset, top_offset)
    output.addPage(page2)
  
outputStream = open(outputfile,'wb') 
output.write(outputStream) 
outputStream.close() 
Editor is loading...