89 lines
2.4 KiB
Python
Executable File
89 lines
2.4 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
parser = argparse.ArgumentParser(
|
|
prog='pdf2book',
|
|
description='pdf2book rearranges pages for signature printing ',
|
|
epilog='AQOABA')
|
|
|
|
#parser.add_argument('pdf-file') # positional argument
|
|
parser.add_argument('pages', type=int) # positional argument
|
|
#parser.add_argument('-s', '--signature') # option that takes a value
|
|
parser.add_argument('-v', '--verbose', action='store_true') # on/off flag
|
|
parser.add_argument('-s', '--signature',
|
|
dest='signature',
|
|
type=int,
|
|
action='store',
|
|
default=1,
|
|
help='')
|
|
args = parser.parse_args()
|
|
|
|
s=args.signature
|
|
if s < 0 :
|
|
# print("signature size must be greater than 0!")
|
|
print("signature size must be positive integer!")
|
|
exit(1)
|
|
pages=args.pages
|
|
#print(pages)
|
|
|
|
lst=[]
|
|
|
|
def get_reorder(page_number: int, signature_size: int):
|
|
panu=page_number
|
|
sisi=signature_size
|
|
|
|
# s| 1 | 2 | 3 | 4 |
|
|
#-----------------------
|
|
#p | | | |
|
|
# 1 | 4 | 8 | 12 | 16 |
|
|
# 2 | 4 | 8 | 12 | 16 |
|
|
# 3 | 4 | 8 | 12 | 16 |
|
|
# 4 | 4 | 8 | 12 | 16 |
|
|
# 5 | 8 | 8 | 12 | 16 |
|
|
# 6 | 8 | 8 | 12 | 16 |
|
|
# 7 | 8 | 8 | 12 | 16 |
|
|
# 8 | 8 | 8 | 12 | 16 |
|
|
# 9 | 12 | 16 | 12 | 16 |
|
|
#10 | 12 | 16 | 12 | 16 |
|
|
#11 | 12 | 16 | 12 | 16 |
|
|
#12 | 12 | 16 | 12 | 16 |
|
|
#13 | 16 | 16 | 24 | 16 |
|
|
|
|
p=page_number
|
|
s=signature_size
|
|
if s == 0:
|
|
s=1
|
|
pages_filled= ( int((p-1)/(4*s)) +1 )*4*s
|
|
s=int(pages_filled/4)
|
|
pages_filled= ( int((p-1)/(4*s)) +1 )*4*s
|
|
|
|
signature_count=int( pages_filled/s/4 )
|
|
#print(signature_count)
|
|
for i in range(signature_count):
|
|
# print("i=",i)
|
|
down=(i+1)*4*s
|
|
# print("down=",down)
|
|
up= (i*4*s)+1
|
|
# print("up",up)
|
|
for count in range(s):
|
|
# print("count=",count)
|
|
lst.append(down); down -= 1
|
|
lst.append(up); up += 1
|
|
lst.append(up); up += 1
|
|
lst.append(down); down -= 1
|
|
# print(lst)
|
|
#print(lst)
|
|
#print(len(lst))
|
|
return lst
|
|
def print_signatures(lst: list,s: int):
|
|
if s==0: s=1
|
|
for i in range( len( lst ) ):
|
|
if ( (i+1) % (4*s) == 0 ):
|
|
print(lst[i])
|
|
elif ( (i+1) % (4) == 0 ):
|
|
print(lst[i], end=' ')
|
|
else:
|
|
print(lst[i],end='.')
|
|
lst=get_reorder(args.pages, args.signature)
|
|
print_signatures(lst,s)
|