Init
commit
e3076a22d0
@ -0,0 +1,4 @@
|
|||||||
|
venv/
|
||||||
|
input.pdf
|
||||||
|
output.pdf
|
||||||
|
.DS_Store
|
@ -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`
|
@ -0,0 +1,2 @@
|
|||||||
|
PyPDF2==2.11.1
|
||||||
|
typing-extensions==4.4.0
|
@ -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)
|
Loading…
Reference in New Issue