Compdigitec Labs

« | Home | »

Concatenating PDFs while padding/extending odd-length PDFs

By admin | October 10, 2020

When concatenating PDFs to consolidate print jobs, it is nice to have single-page or PDFs with odd numbers of pages to not have to share sides with another unrelated document.

There is a short script available at https://unix.stackexchange.com/questions/66418/how-can-i-merge-pdf-files-so-that-each-file-begins-on-an-odd-page-number ; however, it does not appear to be updated for PyPDF2. So here is an update for Python3 and PyPDF2.

Prep:

sudo apt install python3-pypdf2

#!/usr/bin/env python3
# Inspired from https://unix.stackexchange.com/a/66455

import copy
import sys
from PyPDF2 import PdfFileWriter, PdfFileReader

def main():
    if len(sys.argv) < 2:
        print("No input PDFs specified", file=sys.stderr)
        return 1

    pdf_writer = PdfFileWriter()
    output_page_number = 0
    alignment = 2           # to align on even pages

    # So we can close the file objects later.
    # https://stackoverflow.com/questions/44375872/pypdf2-returning-blank-pdf-after-copy
    file_objects = []

    for filename in sys.argv[1:]:
        # Store the file object for closing
        f = open(filename, 'rb')
        file_objects.append(f)

        # Open the input PDF
        pdf_reader = PdfFileReader(f)

        # Add input pages
        for page in pdf_reader.pages:
            pdf_writer.addPage(page)
            output_page_number += 1

        # Add filler pages
        while output_page_number % alignment != 0:
            pdf_writer.addBlankPage()
            output_page_number += 1

    # Write output PDF while input files are still open.
    pdf_writer.write(sys.stdout.buffer)

    # Close open files.
    while len(file_objects) > 0:
        file_objects.pop().close()

    return 0

sys.exit(main())

If you found this article helpful or interesting, please help Compdigitec spread the word. Don’t forget to subscribe to Compdigitec Labs for more useful and interesting articles!

Topics: Linux | 20 Comments »

20 Responses to “Concatenating PDFs while padding/extending odd-length PDFs”

  1. i like this Says:
    August 16th, 2025 at 00:00

    … [Trackback]

    […] Find More Info here on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  2. จองตั๋วรถตู้ Says:
    August 28th, 2025 at 20:44

    … [Trackback]

    […] Find More here to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  3. พลาสติกปูพื้นก่อนเทคอนกรีต Says:
    August 29th, 2025 at 20:40

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  4. วางระบบเน็ตเวิร์ค ระยอง Says:
    September 3rd, 2025 at 01:41

    … [Trackback]

    […] Here you can find 39434 more Info on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  5. คลินิกความงามใกล้ฉัน Says:
    September 30th, 2025 at 03:26

    … [Trackback]

    […] There you can find 40420 more Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  6. สมัครสมาชิกกิฟฟารีน Says:
    October 1st, 2025 at 04:42

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  7. กิฟฟารีน Says:
    October 1st, 2025 at 09:20

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  8. Go X company Says:
    October 2nd, 2025 at 04:52

    … [Trackback]

    […] Read More to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  9. ufa569 Says:
    October 17th, 2025 at 21:34

    … [Trackback]

    […] Read More Info here on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  10. เว็บสล็อต Says:
    October 18th, 2025 at 20:03

    … [Trackback]

    […] Information on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  11. ดูหนังออนไลน์ฟรี Says:
    November 10th, 2025 at 20:09

    … [Trackback]

    […] There you will find 12366 additional Info to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  12. แทงหวยออนไลน์ Says:
    December 7th, 2025 at 23:18

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  13. HILO789 Says:
    December 15th, 2025 at 19:40

    … [Trackback]

    […] Here you can find 49546 additional Information to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  14. ถังน้ำตราบ้าน Says:
    December 15th, 2025 at 20:56

    … [Trackback]

    […] Here you can find 71065 additional Information to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  15. Budayahoki situs scam Says:
    December 21st, 2025 at 10:50

    … [Trackback]

    […] Here you can find 94087 additional Information to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  16. https://mellstroykazino.click Says:
    January 17th, 2026 at 00:50

    … [Trackback]

    […] Here you can find 82566 additional Info on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  17. ไซด์ไลน์ Says:
    February 12th, 2026 at 20:35

    … [Trackback]

    […] Find More Information here on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  18. กระดาษสติ๊กเกอร์ความร้อน Says:
    February 13th, 2026 at 20:48

    … [Trackback]

    […] Information to that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  19. หวยเกาหลี Says:
    February 25th, 2026 at 00:57

    … [Trackback]

    […] Info on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

  20. league88 Says:
    March 13th, 2026 at 22:08

    … [Trackback]

    […] Find More here on that Topic: compdigitec.com/labs/2020/10/10/concatenating-pdfs-while-padding-extending-odd-length-pdfs/ […]

Comments