cfabio

Pdf.py

Jan 18th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.14 KB | None | 0 0
  1. #########################
  2. # PDF
  3. #########################
  4. #extract text from the PDF
  5. import PyPDF2
  6. pdfFile = open(sFilePdf, 'rb')
  7. reader = PyPDF2.PdfFileRead(pdfFile)
  8. reader.numPages
  9. page = reader.getPage(0)
  10. pageText = page.extractText()
  11.  
  12. for pageNum in range(reader.numPages)
  13.     print(reader.getPage(pageNum).extractText())
  14.  
  15. #it cannot write a PDF file but only read the text in it. No images.
  16. #it can re-order or remove pages, or add new pages from a differernt document. It allow therefore editing a page level
  17.  
  18. #take all the pages of the PDF1 and PDF2 and build a new PDF out of it
  19. import PyPDF2
  20. pdfFile1 = open(sFilePdf1, 'rb')
  21. pdfFile2 = open(sFilePdf2, 'rb')
  22. reader1 = PyPDF2.PdfFileRead(pdfFile1)
  23. reader2 = PyPDF2.PdfFileRead(pdfFile2)
  24.  
  25. writer = PyPDF2.PdfFileWriter()   #it exists at this point only in the cache
  26. for pageNum in range(reader1.numPages)
  27.     page = reader1.getPage(pageNum)
  28.     writer.addPage(page)
  29. for pageNum in range(reader2.numPages)
  30.     page = reader2.getPage(pageNum)
  31.     writer.addPage(page)
  32.  
  33. outputFile = open(sCombinedFile, 'wb')
  34. writer.write(outputFile)
  35. outputFile.close()
  36. pdfFile1.close()
  37. pdfFile2.close()
Advertisement
Add Comment
Please, Sign In to add comment