From 9bc4a0d7bfa703fb2ccb67e8290cf517ae72e16b Mon Sep 17 00:00:00 2001 From: Francesco Baldino Date: Fri, 19 May 2023 13:59:03 +0200 Subject: [PATCH] added bookletify --- README.md | 5 +++ bookletify.py | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 126 insertions(+) create mode 100755 bookletify.py diff --git a/README.md b/README.md index dc2cd67..8ce143d 100644 --- a/README.md +++ b/README.md @@ -10,6 +10,11 @@ Questa repository contiene i seguenti script: Consente di impaginare un pdf in modo da poi poterlo stampare "a libretto" +- `bookletify` — [@Fran314](https://git.phc.dm.unipi.it/Fran314) + + Simile a impagina, ma in python. Formatta un pdf in modo da poi poterlo stampare "a libretto" chiudendolo come un libro invece di sovrapporre una metà sull'altra. + Necessita di PyPDF2 installato con pip per funzionare + - `gobbino` — [@BachoSeven](https://git.phc.dm.unipi.it/BachoSeven) + [@Dilillo](http://poisson.phc.dm.unipi.it/~dilillo/Vario/index.html) Permette di scaricare video e/o pdf di qualsiasi corso presente sul sito del professor Gobbino diff --git a/bookletify.py b/bookletify.py new file mode 100755 index 0000000..4739346 --- /dev/null +++ b/bookletify.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python3 + +import os +import sys +from math import ceil +from PyPDF2 import PdfReader, PdfWriter + +def main(): + def print_help(): + print('bookletify') + print() + print('USAGE') + print(' bookletify [OPTIONS] file') + print() + print('DESCRIPTION') + print(' Rearranges the pages of a pdf in order to print it as is (double') + print(' sided, flipped on the short edge) so that you can take it, cut ') + print(' in half and close it like you would do with a book. The result ') + print(' will be the original pdf in the correct order, with the size of ') + print(' half a page') + print(' Especially useful to print pdfs to A5 size on A4 paper') + print() + print('OPTIONS') + print(' -h print help') + print(' -q do not print anything to stdout') + + def parse_argv(): + opts = { + "h": False, + "q": False, + } + filename = None + + if len(sys.argv) < 2: + opts["h"] = True + + for arg in sys.argv[1:]: + if arg.startswith('-'): + for opt in opts: + if opt in arg: + opts[opt] = True + elif not filename: + filename = arg + + return (opts, filename) + + def info(text = '', end='\n'): + if not opts["q"]: + print(text, end=end) + + def prog(curr, max_val): + # Overrides the current terminal line with a progressbar filled at curr/max_val + perc = float(curr) / float(max_val) + remaining = f'{curr:{len(str(max_val))}d}/{max_val}' + (cols, _) = os.get_terminal_size() + max_width = cols - len(remaining) - 3 + + size = min(max_width, max_val, 80) + bar = int(perc * size) + + end = '\n' if curr == max_val else '' + info("\r|" + ("\u2588" * bar) + "_" * (size - bar) + "| " + remaining, end) + + + (opts, input_name) = parse_argv() + if opts["h"] or not input_name: + print_help() + exit(0) + + if not os.path.isfile(input_name): + print(f'file {input_name} does not exist') + exit(1) + + output_name = f'{input_name}-rearranged' + if (input_name.endswith('.pdf')): + output_name = f'{input_name[0:-4]}-rearranged.pdf' + + + # Read the input + reader = PdfReader(input_name) + + # Write the result to a file + middle = PdfWriter() + info("Loading...") + n = len(reader.pages) + # Set m as the smallest multiple of 4 bigger or equal to n + m = ceil(n / 4) * 4 + + for i in range(n): + prog(i, m) + middle.add_page(reader.pages[i]) + + for i in range(m - n): + prog(n + i, m) + middle.add_blank_page() + + prog(m, m) + + output = PdfWriter() + info() + info("Rearranging...") + for i in range(int(m / 4)): + mod = i * 2 + prog(i * 4, m) + output.add_page(middle.pages[m - 1 - mod]) + prog(i * 4 + 1, m) + output.add_page(middle.pages[mod]) + prog(i * 4 + 2, m) + output.add_page(middle.pages[1 + mod]) + prog(i * 4 + 3, m) + output.add_page(middle.pages[m - 2 - mod]) + prog(m, m) + + info() + info("Saving...") + output.write(output_name) + info("Done!") + info() + +if __name__ == "__main__": + main()