Created
June 26, 2015 08:17
-
-
Save vinovator/24a78a968c3a67204bb8 to your computer and use it in GitHub Desktop.
This code is used to combine 2 separate pdf files and create a single pdf file with combined content. The user needs to input the names of 2 pdf source files and the name of the destination file. The 2 source files need to be present in the folder specified in the code.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Python 3 | |
#CombinePDF.py | |
#Gets inputs of 2 PDF file names from user and combines them into 1 | |
import PyPDF2 | |
import os | |
def getFileNameFromUser (file): | |
pdf_file_name = input("Enter {0} name: ".format(file)) | |
if pdf_file_name in os.listdir(): | |
return pdf_file_name | |
else: | |
print ("The file specified is not present in the directory") | |
#Use recursive call to the same function until user gets it right | |
getFileNameFromUser(file) | |
def addPageToWriter(pdfReader, pdfWriter): | |
for pageNum in range(pdfReader.numPages): | |
pageObj = pdfReader.getPage(pageNum) | |
pdfWriter.addPage(pageObj) | |
def getFinalPdfNameFromUser(): | |
return input("Enter the final pdf file name with .pdf extn: ") | |
if __name__ == "__main__": | |
#Change the current folder path to the one containing PDF files | |
pdf_path = ".\PDF" | |
os.chdir(pdf_path) | |
#Get the name of the 2 pdf files from user | |
#file 2 will be appended into file 1 | |
pdf1 = getFileNameFromUser("File 1") | |
pdf2 = getFileNameFromUser("File 2") | |
#Create file objects for both the files | |
pdf1FileObj = open(pdf1, "rb") | |
pdf2FileObj = open(pdf2, "rb") | |
#Pass the file objects to the file reader | |
pdf1Reader = PyPDF2.PdfFileReader(pdf1FileObj) | |
pdf2Reader = PyPDF2.PdfFileReader(pdf2FileObj) | |
#create a Pdf writer object | |
pdfWriter = PyPDF2.PdfFileWriter() | |
#Add individual pages from pdf files to writer object | |
addPageToWriter(pdf1Reader, pdfWriter) | |
addPageToWriter(pdf2Reader, pdfWriter) | |
pdfOutputFileObj = open(getFinalPdfNameFromUser(), "wb") | |
print (".. Appending file 2 to file 1....") | |
pdfWriter.write(pdfOutputFileObj) | |
print ("... done...") | |
pdfOutputFileObj.close() | |
pdf1FileObj.close() | |
pdf2FileObj.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment