hpant

Code for PDF consolidator

Mar 6th, 2018
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.83 KB | None | 0 0
  1. #! python3
  2. # combinePdfs.py - Combines all the PDFs in the current working directory into
  3. # a single PDF.
  4.  
  5. import PyPDF2, os
  6.  
  7. # Get all the PDF filenames.
  8. pdfFiles = []
  9. for filename in os.listdir('.'):
  10.     if filename.endswith('.pdf'):
  11.         pdfFiles.append(filename)
  12.  
  13. pdfWriter = PyPDF2.PdfFileWriter()
  14.  
  15. # Loop through all the PDF files.
  16. for filename in pdfFiles:
  17.     pdfFileObj = open(filename, 'rb')
  18.     pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
  19. # password of each PDF file is below
  20.     pdfReader.decrypt('mtgy')
  21.  
  22.     # Loop through all the pages (except the first) and add them.
  23.     for pageNum in range(0, 0):
  24.         pageObj = pdfReader.getPage(pageNum)
  25.         pdfWriter.addPage(pageObj)
  26.  
  27. # Save the resulting PDF to a file.
  28. pdfOutput = open('consolidat.pdf', 'wb')
  29. pdfWriter.write(pdfOutput)
  30. pdfOutput.close()
Add Comment
Please, Sign In to add comment