Merge PDF files


[‘24234’, ‘01441’, ‘09468’]

mergers = [PdfFileMerger() for i in range(len(unique_new_names))]
for unique_name,i in zip(unique_new_names,range(len(unique_new_names))):

for file, PDF_name in zip(all_files,all_PDF_names):
    if PDF_name.startswith(unique_name):
        mergers[i].append(open(file, 'rb'))

OSError: [Errno 22] Invalid argument

I am trying to merge PDF files with the same name, can anyone help?
How do create and use class objects dynamically?

Hi Sebastian,

I have no experience with the PdfFileMerger library/class, but it looks like, that you just need to create ONE instance of said class:

merger = PdfFileMerger()

And then you can append file objects (!).

So, create a file object by opening the belonging pdf file:

fileHandle = open(“example.pdf”, “rb”) #whereby the “b” just is a guess, if it does not work, try it without the “b”

then you can append said file object by:

merger.append( fileHandle )

I did not try that out, but that’s how objects work normally… :slight_smile:

Cheers, Dominik