Advertisement
Guest User

pdf_page_extraction

a guest
Jun 5th, 2020
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.85 KB | None | 0 0
  1. from PyPDF2 import PdfFileWriter, PdfFileReader
  2. import sys
  3. import os
  4.  
  5.  
  6. #directoryout = 0
  7. class EP:
  8.     # Drag a pdf file to this script and it will separate the pages and put them in their own direcrtory
  9.     def extract_pages(self, path):
  10.         self.pdfpath = path
  11.         inputpdf = PdfFileReader(open(path, "rb"))
  12.         self.inputpdf = inputpdf
  13.         # Directory path is the same name as the file without the extention
  14.         self.directoryout = path.strip('.pdf')
  15.         os.mkdir(self.directoryout)
  16.  
  17.         for i in range(inputpdf.numPages):
  18.             output = PdfFileWriter()
  19.             output.addPage(inputpdf.getPage(i))
  20.             with open("%s\\document-page%s.pdf" % (self.directoryout, i+1), "wb") as outputStream:
  21.                 output.write(outputStream)
  22.  
  23.  
  24. if __name__ == "__main__":
  25.     a=EP()
  26.     a.extract_pages(sys.argv[1])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement