From b75dfc72ff05b47e5c75f29f311137dca9958832 Mon Sep 17 00:00:00 2001 From: Elena ``of Valhalla'' Grandi Date: Wed, 26 Nov 2025 09:07:05 +0100 Subject: Use pypdf to detect the number of pages in a pdf, if available --- page_manipulation/a5_book.py | 38 ++++++++++++++++++++++++++------------ page_manipulation/a6_book.py | 44 ++++++++++++++++++++++++++++++-------------- 2 files changed, 56 insertions(+), 26 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 '-' is supported" ) parser.add_argument( - "pagespec", - help="Page selection. At the moment only '-' 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: 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, @@ -37,10 +42,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", @@ -54,8 +55,13 @@ def get_parser(): "multiple of 64", ) parser.add_argument( - "pagespec", - help="Page selection. At the moment only '-' is supported" + "--pagespec", + help="Page selection. At the moment only '-' 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 -- cgit v1.2.3