diff options
Diffstat (limited to 'page_manipulation/a6_book.py')
| -rwxr-xr-x | page_manipulation/a6_book.py | 44 |
1 files changed, 30 insertions, 14 deletions
diff --git a/page_manipulation/a6_book.py b/page_manipulation/a6_book.py index 550cb5d..cbb9d44 100755 --- a/page_manipulation/a6_book.py +++ b/page_manipulation/a6_book.py @@ -4,6 +4,11 @@ import argparse import os.path import subprocess +try: + import pypdf +except ImportError: + pypdf = None + SIGNATURE = ( 15, 0, 13, 2, @@ -38,10 +43,6 @@ SIGNATURE_64 = ( def get_parser(): parser = argparse.ArgumentParser() parser.add_argument( - "--file", '-f', - help="Name of the PDF file", - ) - parser.add_argument( "--double", "-d", action="store_true", help="Put two signatures on 4 A4 sheet. Pages will be a " @@ -54,8 +55,13 @@ def get_parser(): "multiple of 64", ) parser.add_argument( - "pagespec", - help="Page selection. At the moment only '<start>-<end>' is supported" + "--pagespec", + help="Page selection. At the moment only '<start>-<end>' is supported", + default=None, + ) + parser.add_argument( + "file", + help="Name of the PDF file", ) return parser @@ -63,14 +69,24 @@ def get_parser(): def main(): parser = get_parser() args = parser.parse_args() - pspec = args.pagespec.split('-') - try: - start = int(pspec[0]) - end = int(pspec[1]) - except ValueError: - parser.print_usage() - except IndexError: - parser.print_usage() + + + if not args.pagespec: + if not pypdf: + print("Detecting the number of pages in the pdf requires pypdf") + sys.exit(1) + reader = pypdf.PdfReader(args.file) + start = 1 + end = end = len(reader.pages) + else: + pspec = args.pagespec.split('-') + try: + start = int(pspec[0]) + end = int(pspec[1]) + except ValueError: + parser.print_usage() + except IndexError: + parser.print_usage() if args.double: signature = SIGNATURE_32 |
