0

New here - couldnt find answer to this but please direct if already answered.

I am looking to create a set of new PDFS by merging a "main" PDF to a list of other "sub" PDFs (say 5 for example) and get an output of 5 pdfs with Main+sub1, Main+sub2.

This is what I have so far. But it is creating 1 pdf with all the pdfs (in the directory) together as one. What I would like is for it to create separate ones as described above - not sure what do add here for it to iterate through each one to do what I want:

    import PyPDF2
    import glob, os
    import shutil

    from PyPDF2 import PdfFileMerger

    dir="address"

    os.chdir(dir)

    x = [dir+"/"+a for a in os.listdir(dir) if a.endswith(".pdf")]
    print(x)

    os.listdir

    merger = PdfFileMerger()

    for pdf in x:
        merger.append(open(pdf, 'rb'))

    with open("result.pdf", "wb") as fout:
        merger.write(fout)
Amphi
  • 1
  • 1
  • Welcome to SO... It would make it easier to help you, if you explained what is happening when you run your code... Post any stacktraces if throwing errors... explain the expected behaviour and the actual behaviour... Also this may answer your question https://stackoverflow.com/questions/3444645/merge-pdf-files – Paddy Popeye Nov 07 '20 at 09:06
  • Thanks! Its currently creating 1 file with all of the pdfs in the directory together. Updating my post as well, appreciate the note/tip – Amphi Nov 08 '20 at 16:16

0 Answers0