首页 > 代码库 > python编程快速上手之第13章实践项目参考答案(13.6.1)

python编程快速上手之第13章实践项目参考答案(13.6.1)

import os,PyPDF2
os.chdir(D:\\My Documents)
for folderName, subfolders, filenames in os.walk(D:\\My Documents):
  for file in filenames:
    if file.endswith(.pdf):
       pdfFile = open(file, rb)
       pdfReader = PyPDF2.PdfFileReader(pdfFile)
       pdfWriter = PyPDF2.PdfFileWriter()
       for pageNum in range(pdfReader.numPages):
         pdfWriter.addPage(pdfReader.getPage(pageNum))
       pdfWriter.encrypt(123456)
       resultPdf = open(file +encrypted.pdf, wb)
       pdfWriter.write(resultPdf)
       resultPdf.close()

 

python编程快速上手之第13章实践项目参考答案(13.6.1)