added bookletify

removeproofs
Francesco Baldino 2 years ago
parent 46b8ebbc95
commit 9bc4a0d7bf

@ -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

@ -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()
Loading…
Cancel
Save