Python - Merge PDF Files
Python - Merge PDF Files
CODE
#PDF Module that helps in merging the files
from PyPDF2 import PdfFileMerger
#The Module that helps in looping through the folder.
import os
#This is the folder where the PDF files to be merged will be placed.
inputFolder = "C:\\Test\\Simple Kettle\\Programs\\work\\input"
#This is the output folder where the merged file will be placed.
outputFolder = "C:\\Test\\Simple Kettle\\Programs\\work\\output"
#This is the function to get the names of the files available in the input folder
def getFiles():
#Declare an empty list
filesList = []
#Loop through the files
for f in os.listdir(inputFolder):
#Check if it ends with pdf
if f.endswith(".pdf"):
#If it is a pdf file, append it to the filesList
filesList.append(f)
#Finally return the filesList
return filesList
def mergePDFFiles():
#Call the getFiles function and that will return the list of pdf files.
files = getFiles()
#Instantiate the Merger object.
m = PdfFileMerger()
#Loop through the Files List
for f in files:
#append the file to the Merge object.
m.append(inputFolder + "\\" + f)
#Write the merger object contents to the result.pdf file in the output folder.
m.write(outputFolder + "\\" + "result.pdf")
#Close the merger object.
m.close()
#Print a success message to the Console.
print("The PDF Files are successfully merged")
mergePDFFiles()
Comments
Post a Comment