Last active
June 10, 2023 14:26
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- 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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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