Skip to content

Instantly share code, notes, and snippets.

@kirienko
Forked from tshrinivasan/split-page.py
Last active March 3, 2024 08:59
Show Gist options
  • Save kirienko/55436c7c68088a98ffe54a68c7bd7b4c to your computer and use it in GitHub Desktop.
Save kirienko/55436c7c68088a98ffe54a68c7bd7b4c to your computer and use it in GitHub Desktop.
Split a PDF vertically, used for imposed scanned double sided PDF pages
# see https://stackoverflow.com/questions/51973437/reverse-pdf-imposition
import copy
import sys
import math
import pyPdf
"""
Reverse imposing:
--A3-- --A4--
[12| 1] [1]
[ 2|11] [2]
[10| 3] => [3]
... ...
[ 6| 7] [6]
[7]
...
[12]
"""
def split_pages(src, dst):
src_f = file(src, 'r+b')
dst_f = file(dst, 'w+b')
input_PDF = pyPdf.PdfFileReader(src_f)
num_pages = input_PDF.getNumPages()
first_half, second_half = [], []
for i in range(num_pages):
p = input_PDF.getPage(i)
q = copy.copy(p)
q.mediaBox = copy.copy(p.mediaBox)
x1, x2 = p.mediaBox.lowerLeft
x3, x4 = p.mediaBox.upperRight
x1, x2 = math.floor(x1), math.floor(x2)
x3, x4 = math.floor(x3), math.floor(x4)
x5, x6 = math.floor(x3/2), math.floor(x4/2)
if x3 > x4:
# horizontal
p.mediaBox.upperRight = (x5, x4)
p.mediaBox.lowerLeft = (x1, x2)
q.mediaBox.upperRight = (x3, x4)
q.mediaBox.lowerLeft = (x5, x2)
else:
# vertical
p.mediaBox.upperRight = (x3, x4)
p.mediaBox.lowerLeft = (x1, x6)
q.mediaBox.upperRight = (x3, x6)
q.mediaBox.lowerLeft = (x1, x2)
if i in range(1,num_pages+1,2):
first_half += [p]
second_half += [q]
else:
first_half += [q]
second_half += [p]
output = pyPdf.PdfFileWriter()
for page in first_half + second_half[::-1]:
output.addPage(page)
output.write(dst_f)
src_f.close()
dst_f.close()
if len(sys.argv) < 3:
print("\nusage:\n$ python reverse_impose.py input.pdf output.pdf")
sys.exit()
input_file = sys.argv[1]
output_file = sys.argv[2]
split_pages(input_file,output_file)
@rodhfr
Copy link

rodhfr commented Sep 14, 2023

which package is this pyPdf? I've encountered a library called pypdf, but not pyPdf

@kirienko
Copy link
Author

which package is this pyPdf? I've encountered a library called pypdf, but not pyPdf

I think it is the same thing, but it was spelt diffirently back then.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment