home · contact · privacy
Allow page ranges beyond input, pad output with blank pages. master
authorChristian Heller <c.heller@plomlompom.de>
Thu, 14 Dec 2023 00:08:49 +0000 (01:08 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Thu, 14 Dec 2023 00:08:49 +0000 (01:08 +0100)
README.txt
bookmaker.py

index 57fc9cfa137bd355ef45b86be16a927518962076..f27bf8424a38af4b50b010c2095b5c8cae77e519 100644 (file)
@@ -79,4 +79,6 @@ FURTHER NOTES:
 
 For arguments like -p, page numbers are assumed to start with 1 (not 0, which is treated as an invalid page number value).
 
+If -p defines a page range beyond the source document's last page, the remaining pages will be created blank.
+
 The target page shape so far is assumed to be A4 in portrait orientation; bookmaker.py normalizes all pages to this format before applying crops, and removes any source PDF /Rotate commands (for their production of landscape orientations).
index 2440747b02a70b3a8ca5cfc1bf62501d7d2cb49a..bc26619b932c3c1dde7ae1d3f419ca9ef7291f70 100755 (executable)
@@ -211,11 +211,12 @@ def read_inputs_to_pagelist(args_input_file, args_page_range):
         if args_page_range and len(args_page_range) > i:
             range_string = args_page_range[i]
         start_page, end_page = parse_page_range(range_string, reader.pages)
-        if end_page > len(reader.pages):  # no need to test start_page cause start_page > end_page is checked above
-            raise HandledException(f"-p: page range goes beyond pages of input file: {range_string}")
         for old_page_num in range(start_page, end_page):
             new_page_num += 1
-            page = reader.pages[old_page_num]
+            if old_page_num >= len(reader.pages):
+                page = pypdf.PageObject.create_blank_page(width=A4_WIDTH, height=A4_HEIGHT)
+            else:
+                page = reader.pages[old_page_num]
             pages_to_add += [page]
             print(f"-i, -p: read in {input_file} page number {old_page_num+1} as new page {new_page_num}")
     return pages_to_add, opened_files