commit e3076a22d026a37f5e817959c8dfd39945a680d1 Author: kam (from the studio) Date: Wed Nov 16 16:30:45 2022 +0100 Init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..dbc1026 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +venv/ +input.pdf +output.pdf +.DS_Store \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..15a6c87 --- /dev/null +++ b/README.md @@ -0,0 +1,21 @@ +# Split pdf pages + +![zac cover](cover.png) + +There is this book i want to print, but the pages are already coupled 2 by 2 and so the booklet printing doesn't work. + +This script uses [pypdf2](https://pypdf2.readthedocs.io/en/latest/user/cropping-and-transforming.html) to crop and transform the a4 horizontal pages of the pdf into 2 a5 vertical ones. + +Good way to procrastinate eh + +## Usage + +First install the requirements (actually just PyPDF2) + +`pip install -r requirements.txt` + +The script takes a `input.pdf` and gives out a `output.pdf`. + +Then run the code. + +`python3 zac.py` diff --git a/cover.png b/cover.png new file mode 100644 index 0000000..7b7f419 Binary files /dev/null and b/cover.png differ diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..bac385f --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +PyPDF2==2.11.1 +typing-extensions==4.4.0 diff --git a/zac.py b/zac.py new file mode 100644 index 0000000..08a6076 --- /dev/null +++ b/zac.py @@ -0,0 +1,31 @@ +from PyPDF2 import PdfWriter, PdfReader, PageObject + +reader = PdfReader('input.pdf') +writer = PdfWriter() + +for index, page in enumerate(reader.pages): + if index != 0 and index != len(reader.pages)-1: + + offset = page.mediabox.right / 2 + + # hack to clone the page as a copy not reference + # https://github.com/py-pdf/PyPDF2/issues/100#issuecomment-378890266 + left_page = PageObject.createBlankPage(None, page.mediaBox.getWidth(), page.mediaBox.getHeight()) + left_page.mergePage(page) + + left_page.mediabox.right = offset + writer.add_page(left_page) + + + right_page = PageObject.createBlankPage(None, page.mediaBox.getWidth(), page.mediaBox.getHeight()) + right_page.mergePage(page) + + right_page.mediabox.left = offset + writer.add_page(right_page) + + else: + writer.add_page(page) + + +with open("output.pdf", "wb") as fp: + writer.write(fp) \ No newline at end of file