Sometimes when I try to combine Pdf files, a page would be duplicated where the next page
is supposed to be.
Here is the code I use to combine the pdf files.
pdfFiles = []
for filename in os.listdir(self.source):
if filename.endswith('.pdf'):
pdfFiles.append(os.path.join(self.source,filename))
pdfFiles.sort(key = str.lower)
pdfWriter = PyPDF2.PdfWriter()
for filename in pdfFiles:
#print(filename)
pdfFileObj = open(filename,'rb')
pdfReader = PyPDF2.PdfReader(pdfFileObj)
#for pageNum in range(0, pdfReader.numPages):
for pageNum in range(len(pdfReader.pages)):
#pageObj = pdfReader.getPage(pageNum)
pageObj = pdfReader.pages[pageNum]
pdfWriter.add_page(pageObj)
pdfFileObj.close()
fileout = CombineName
#print(fileout)
pdfOutput = open(self.destination + fileout,'wb')
pdfWriter.write(pdfOutput)
pdfOutput.close()
The number pages are correct but some pages are duplicated and the other page is missing.
New contributor
Reach Miami is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.