Skip to content

Instantly share code, notes, and snippets.

@kintaro1981
Last active June 10, 2023 14:26
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kintaro1981/eb6cfc6f40a7fb39744f5ae630d58fd5 to your computer and use it in GitHub Desktop.
Save kintaro1981/eb6cfc6f40a7fb39744f5ae630d58fd5 to your computer and use it in GitHub Desktop.
This script splits each pdf page in half vertically and merge them in a single PDF in order. It asks how many pages do you want to skip to avoid splitting a book cover or a specific number of initial pages.
# -*- coding: utf-8 -*-
"""
This script splits each pdf page in half vertically and merge them in a single PDF in order.
It asks how many pages do you want to skip to avoid splitting a book cover or a specific number of initial pages.
Usage:
python cutpdfpages.py <filename.pdf> <newfilename.pdf>
Inspired by: https://github.com/anadeangelo/pdf_crop
"""
import sys
import copy
from PyPDF2 import PdfWriter, PdfReader
file = str(sys.argv[1])
newfile = str(sys.argv[2])
with open(file, "rb") as pdf1:
pdf = PdfReader(pdf1)
output = PdfWriter()
numpages = pdf.getNumPages()
page2skip = int(input('Insert how many pages do you want to skip: '))
for i in range(page2skip, numpages):
page = pdf.pages[i]
pagebis = copy.copy(page)
page.mediabox.upper_left = (page.mediabox.right / 2, page.mediabox.top,)
output.add_page(page)
pagebis.mediabox.upper_right = (pagebis.mediabox.right / 2, pagebis.mediabox.top,)
output.add_page(pagebis)
with open(newfile, "wb") as newpdf:
output.write(newpdf)
@CHIJUI0128
Copy link

After I cut off the pdf, however this seems to just hide as the size is still the same and the cutout part can still be called out

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