In the above code, we append only first 3 pages of each document to create a single document. If you want to append only specific pages and not the entire document, you can use pages keyword argument and pass a tuple of the format (start, end ) to specify the page range to be appended. The above examples append one PDF fully with other PDF documents. In this case, we use merge function to insert every pdf after the 2nd page. In this case, you can specify the page number after which the insertion needs to take place. It allows you to specify an insertion point in output file. You can also use merge() function to append pdf file. So you may want to use full paths instead of relative paths. Please note, if you mention only filenames in pdfs list above, python code will look for them relative to its location. Lastly, we call close() function to close both input and output files. Finally, we call write() function to write the appended content into a single file result.pdf. We create a PdfFileMerger() object first, and then basically loop through the list containing filenames, appending each of them to the PdfFileMerger() object. In the above code, we append file1.pdf, file2.pdf, and file3.pdf into result.pdf file. In this case, we import PDfFileMerger from PyPDF2 and use append() to append one file to another. Let us say you have PDF files file1.pdf, file2.pdf, and file3.pdf. PyPDF2 provides several ways to merge PDF files.
Open terminal and run the following command to install PyPDF2 in python. For this purpose, we will use PyPDF2 library. Here are the different ways to merge PDF files using Python.
How to install pypdf2 how to#
In this article, we will learn how to merge PDF files using python. Sometimes you may need to combine multiple PDF files into single file. Python provides numerous ways to work files including PDF files.