# Merge PDF files

**URL:** <https://discuss.python.org/t/merge-pdf-files/6678>\
**Category:** Python Help\
**Created:** [January 16, 2021, 12:22am UTC](https://discuss.python.org/t/merge-pdf-files/6678 "2021-01-16T00:22:41Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sebastian](https://avatars.discourse-cdn.com/v4/letter/s/4da419/32.png) [@sebastian](https://discuss.python.org/u/sebastian)\
**Post date:** [January 16, 2021, 12:22am UTC](https://discuss.python.org/t/merge-pdf-files/6678/1 "2021-01-16T00:22:41Z")

</div>

> print(unique\_new\_names)
> 
> [‘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?

---

<div class="post-metadata">

**Author:** ![Blackward](https://avatars.discourse-cdn.com/v4/letter/b/f08c70/32.png) [@Blackward](https://discuss.python.org/u/Blackward)\
**Post date:** [April 25, 2021, 5:46am UTC](https://discuss.python.org/t/merge-pdf-files/6678/2 "2021-04-25T05:46:06Z")

</div>

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… 🙂

Cheers, Dominik
