Skip to content

Instantly share code, notes, and snippets.

@AlexDenisov
Created July 26, 2020 08:28
Show Gist options
  • Save AlexDenisov/ce9c9dcf5e05dc5d89ba02e624bad582 to your computer and use it in GitHub Desktop.
Save AlexDenisov/ce9c9dcf5e05dc5d89ba02e624bad582 to your computer and use it in GitHub Desktop.
Splits a PDF file for manual double-side printing
#!/usr/bin/env python3
import sys, os
from PyPDF2 import PdfFileReader, PdfFileWriter
inputname = sys.argv[1]
pdf = PdfFileReader(inputname)
numpages = pdf.getNumPages()
if numpages % 2 == 1:
numpages = numpages - 1
pdf_writer = PdfFileWriter()
pdf_writer.addPage(pdf.getPage(numpages))
with open("last.pdf", "wb") as out:
pdf_writer.write(out)
odd = PdfFileWriter()
even = PdfFileWriter()
for i in range(numpages):
page = pdf.getPage(i)
if (i % 2) == 1:
odd.addPage(page)
else:
even.addPage(page)
with open("odd.pdf", "wb") as out:
odd.write(out)
with open("even.pdf", "wb") as out:
even.write(out)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment