#! python 3
# combinePdfs.py - Combines all the PDFs in the current working directory into a string PDF.
import PyPDF2,os
# Get all the PDF fileNames
pdfFiles = []
for filenamein os.listdir('.'):
if filename.endswith('.pdf'):
pdfFiles.append(filename)
pdfFiles.sort(key = str.lower)
pdfWriter = PyPDF2.PdfFileWriter()
# LOOP through all the PDF files
for filename in pdfFiles:
pdfFileObj =open(filename, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
# LOOP through all the pages (except the first) and add them.
for pageNum in range(1,pdfReader.numPages):
pageObj = pdfReader.getPage(pageNum)
pdfWriter.addPage(pageObj)
# Save the resulting PDF to a file.
pdfOutputFile =open('all.pdf','wb')
pdfWriter.write(pdfOutputFile)
pdfOutputFile.close()
遇到的问题:
1.Python中List的sort的用法:pdfFiles.sort(key = str.lower)
2.Python中路径拼接:pdfFileObj =open(filename, 'rb')