PDF #合并 #PDF合并

  1. #! python3
  2. # combinePdfs.py - Combines all the PDFs in the current working directory into
  3. # a single PDF.
  4. import os
  5. import PyPDF2
  6. # Get all the PDF filenames.
  7. # file_path = './13-pdf-word/.'
  8. pdfFiles = []
  9. for filename in os.listdir('.'):
  10. if filename.endswith('.pdf'):
  11. pdfFiles.append(filename)
  12. pdfFiles.sort()
  13. pdfWriter = PyPDF2.PdfFileWriter()
  14. # Loop through all the PDF files.
  15. for filename in pdfFiles:
  16. pdfFileObj = open(filename, 'rb')
  17. pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
  18. # Loop through all the pages (except the first) and add them.
  19. for pageNum in range(1, pdfReader.numPages):
  20. pageObj = pdfReader.getPage(pageNum)
  21. pdfWriter.addPage(pageObj)
  22. # Save the resulting PDF to a file.
  23. pdfOutput = open('allminutes.pdf', 'wb')
  24. pdfWriter.write(pdfOutput)
  25. print("combined ok!")
  26. pdfOutput.close()