Quite a few people have informed me through reviews or emails that they found it very useful. If you are looking for a super efficient way to learn Python scripting for ABAQUS in just 10 days, go ahead and buy my book. Automate boring stuff with Python – A great book –.PyPDF2 documentation – to explore further options –.So this brings us to the end of this one off-topic post about merging multiple PDF files into a single file. If you have a method or a script to take care of that part, then obviously method 2 is very efficient. Method 2 might look very efficient to you – But it has it’s catch about the file names. MergedObject.write("mergedfilesoutput.pdf") # Write all the files into a file which is named as shown below MergedObject.append(PdfFileReader('6_yuddhakanda_' + str(fileNumber)+ '.pdf', 'rb')) # Loop through all of them and append their pages # I had 116 files in the folder that had to be merged into a single document Without much further ado, let’s have a look at the code.įrom PyPDF2 import PdfFileMerger, PdfFileReader And remember that these files are placed in Python’s working directory.Īs you can see, the file names follow a pattern which makes my job very very easy to loop through them. Before we go there, I will show how my input files are named. Write the appended files into an output document and specify a name for it.Loop through all the files that have to be merged and append them.Import PdfFileMerger and PdfFileReader tools.If all the PDF files that you want to merge follow a naming convention for their file names.When you have a lot of PDF files ( I mean a loooot – Like for example, hundreds of PDF files or even more).It’s my favorite and it uses the PdfFileMerger module. This method is more elegant and has just 5 lines of code. # Close all the files - Created as well as opened PdfOutputFile = open('MergedFiles.pdf', 'wb') # Now that you have copied all the pages in both the documents, write them into the a new document # Loop through all the pagenumbers for the second documentįor pageNum in range(pdf2Reader.numPages): # Loop through all the pagenumbers for the first documentįor pageNum in range(pdf1Reader.numPages): # Create a new PdfFileWriter object which represents a blank PDF document Pdf2Reader = PyPDF2.PdfFileReader(pdf2File) Pdf1Reader = PyPDF2.PdfFileReader(pdf1File) Pdf2File = open('SecondInputFile.pdf', 'rb') Pdf1File = open('FirstInputFile.pdf', 'rb') # Open the files that have to be merged one by one If you find the above sequence difficult to understand, have a look at the code below.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |