Skip to content

Instantly share code, notes, and snippets.

@yatmanov
Last active March 5, 2023 11:57
Show Gist options
  • Save yatmanov/e2c36e55bc84f081237e4236d55da060 to your computer and use it in GitHub Desktop.
Save yatmanov/e2c36e55bc84f081237e4236d55da060 to your computer and use it in GitHub Desktop.
# pip install PyPDF2==1.26.0
#
from PyPDF2 import PdfFileReader, PdfFileWriter
from PyPDF2.pdf import PageObject
reader = PdfFileReader(open('some.pdf', 'rb'))
writer = PdfFileWriter()
p = reader.getPage(1)
h = p.mediaBox.getWidth()
w = p.mediaBox.getHeight()
for page_num in list(range(90, 110, 2):
p = PageObject.createBlankPage(None, h, w)
p.mergeRotatedScaledTranslatedPage(reader.getPage(page_num), -90, w/h, 0, w)
p.mergeRotatedScaledTranslatedPage(reader.getPage(page_num+1), -90, w/h, h/2, w)
writer.addPage(p)
with open('out.pdf', 'wb') as o:
writer.write(o)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment