This is one off-post, irrelevant to my blog's intense focus.
Yes – This article would non be about Exhaustible Element Method(FEM) Beaver State whatever of the concepts joint with it.
Confluence multiple PDFs into a single papers is one activity which nigh of United States have to do. Almost on a daily fundament or along a weekly or monthly basis. There are of course many another websites which offer this as a service. The ones which allow you to merge PDFs gratis often have few limits. Either based on number of files or the time 'tween all meeting operation.
What if you can write a Python script that can do this for you?
Sounds great right? Hold reading.
In this clause, I am presenting two different methods for merging many PDF files into a single written document. Using the Python tool kit – PyPDF2.
Before we go further, I underscore that there is zero "indefinite-method-fits-all" approach. And I manage non claim that these methods are the best. These are two methods that have worked fine for me so remote. So, I thought that I would partake it in this platform.
Prerequisites before you try either of these methods:
Make sure that
- You have installed latest version of Python (that's obvious, duh!)
- You have installed the PyPDF2 tool kit
- Saved the PDF files that you want to merge in Python's working directory. Of flow, you can switch the directory exploitation Python encipher. For simplicity of code, I am placing the PDF files on the working directory for these 2 methods that I am going to present here..
Method 1:
This method is directly interpreted from Chapter 13 of the book of account "Automatize the Boring Englut with Python" by Al Sweigart.
When is method 1 suitable?
- When you have lesser number of files
- When the group of files to be merged do non have a ordinary computer file nam pattern
How this method works?
In the chase sequence.
- Implication the PyPDF2 tool kit which has the tools that we need for playing with PDFs
- Open each and every file by entering the filename
- Read each and every filing cabinet which was opened in Ill-use 2 using PdfFileReader
- Create a blank PDF file cabinet using PdfFileWriter where you can store the merged output
- Loop through every page in every file which was read in Step 3 using for loop and imitate wholly the information
- Give a name for the output file so paste all the derived information in Whole tone 5
- Close all the files
If you find the above sequence hard-fought to understand, have a see at the code below. Python is very proofreader-amiable. Indeed I hope you would get the idea.
importee PyPDF2 # Visible the files that experience to glucinium unified one by extraordinary pdf1File = open('FirstInputFile.pdf', 'atomic number 37') pdf2File = open('SecondInputFile.pdf', 'rb') # Read the files that you have opened pdf1Reader = PyPDF2.PdfFileReader(pdf1File) pdf2Reader = PyPDF2.PdfFileReader(pdf2File) # Create a new PdfFileWriter object which represents a blank PDF text file pdfWriter = PyPDF2.PdfFileWriter() # Grummet through all the pagenumbers for the first papers for pageNum in range(pdf1Reader.numPages): pageObj = pdf1Reader.getPage(pageNum) pdfWriter.addPage(pageObj) # Loop done all the pagenumbers for the second document for pageNum in range(pdf2Reader.numPages): pageObj = pdf2Reader.getPage(pageNum) pdfWriter.addPage(pageObj) # Now that you hold copied whol the pages in both the documents, write them into the a new document pdfOutputFile = open('MergedFiles.pdf', 'wb') pdfWriter.write(pdfOutputFile) # Close together all the files - Created as well as opened pdfOutputFile.close() pdf1File.close() pdf2File.close() Method 2:
This method is more elegant and has just now 5 lines of code. It's my favorite and it uses the PdfFileMerger module.
When is method 2 suitable?
- When you consume a lot of PDF files ( I mean a loooot – Like for instance, hundreds of PDF files or even more)
- If all the PDF files that you want to merge follow a naming normal for their charge name calling.
How this method whole kit and caboodle?
In the favourable sequence.
- Meaning PdfFileMerger and PdfFileReader tools
- Iteration through all the files that have got to be united and append them
- Write the appended files into an output document and specify a name for it.
That's it. It's spatula-shaped but strong.
So get's check over the code now. Before we blend there, I will evidenc how my input files are named. And remember that these files are placed in Python's working directory.
As you can see, the file name calling espouse a pattern which makes my subcontract very very easy to loop done them.
Okay. Without much advance ado, let's take a look at the encipher.
from PyPDF2 import PdfFileMerger, PdfFileReader # Call the PdfFileMerger mergedObject = PdfFileMerger() # I had 116 files in the leaflet that had to be merged into a single document # Loop through with altogether of them and supplement their pages for fileNumber in kitchen stove(1, 117): mergedObject.add(PdfFileReader('6_yuddhakanda_' + str(fileNumber)+ '.pdf', 'Rb')) # Write all the files into a file which is named as shown below mergedObject.publish("mergedfilesoutput.pdf") Method acting 2 might look very effectual to you – But it has it's catch about the file name calling. If you have a method acting or a script to see of that share, then evidently method 2 is very efficient.
So this brings the States to the end of this one off-issue post about merging multiple PDF files into a file. I hope IT was useful.
Have a great twenty-four hours.
Prost! ~ Renga
Any links to ponder:
- PyPDF2 documentation – to explore further options – https://pythonhosted.org/PyPDF2/
- Automatise oil production stuff with Python – A great book – https://automatetheboringstuff.com/#toc
how to put multiple pdfs in one document
Source: https://caendkoelsch.wordpress.com/2019/05/10/merging-multiple-pdfs-into-a-single-pdf/