diff options
| author | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2025-11-26 09:07:05 +0100 |
|---|---|---|
| committer | Elena ``of Valhalla'' Grandi <valhalla@trueelena.org> | 2025-11-26 09:51:58 +0100 |
| commit | b75dfc72ff05b47e5c75f29f311137dca9958832 (patch) | |
| tree | 7d07e439c2808c28398f4c0d983b105c9589f724 /page_manipulation/a5_book.py | |
| parent | 03ed102ac297377fcf47492bd278acd1e413a4dd (diff) | |
Use pypdf to detect the number of pages in a pdf, if available
Diffstat (limited to 'page_manipulation/a5_book.py')
| -rwxr-xr-x | page_manipulation/a5_book.py | 38 |
1 files changed, 26 insertions, 12 deletions
diff --git a/page_manipulation/a5_book.py b/page_manipulation/a5_book.py index 846ca86..1e004e0 100755 --- a/page_manipulation/a5_book.py +++ b/page_manipulation/a5_book.py @@ -4,6 +4,11 @@ import argparse import os.path import subprocess +try: + import pypdf +except ImportError: + pypdf = None + SIGNATURE = ( 15, 0, @@ -20,12 +25,12 @@ SIGNATURE = ( def get_parser(): parser = argparse.ArgumentParser() parser.add_argument( - "--file", '-f', - help="Name of the PDF file", + "--pagespec", + help="Page selection. At the moment only '<start>-<end>' is supported" ) parser.add_argument( - "pagespec", - help="Page selection. At the moment only '<start>-<end>' is supported" + "file", + help="Name of the PDF file", ) return parser @@ -33,14 +38,23 @@ 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() pages = [str(i) for i in range(start, end+1)] if len(pages) % 16 != 0: |
