PyPDF2 placing pages 2-9 at random locations in merged .pdf; everything else in the correct order

49 views Asked by At

I'm using this code to merge 94 individual 1-page .pdf documents into a book.

from glob import glob
from PyPDF2 import PdfMerger


def pdf_merge():
    """Merges individual .pdfs into one large .pdf"""
    merger = PdfMerger()
    allpdfs = [a for a in glob("*.pdf")]
    [merger.append(pdf) for pdf in allpdfs]
    with open("book.pdf", "wb") as new_file:
        merger.write(new_file)


if __name__ == "__main__":
    pdf_merge()

This script is placed in a directory with the individual "pages" of the final book file (94 individual .pdf files consisting of one page each). Each filename is formatted as page_X.pdf where "X" is the number of the page, beginning with 1 and ending with 94, i.e. "page_1" through "page_94".

Everything runs smoothly and I get a .pdf at the end called book.pdf. The majority of the pages are in the correct order. However strangely pages 2-9 are scattered at seemingly random intervals throughout. I.e., page 1 is correct, then 2-9 are missing, so the 2nd page is page 10; and as you continue everything is in the right order except 2-9 which you stumble upon at times.

Thanks for your help.

0

There are 0 answers