《Python编程快速上手—让繁琐工作自动化》第13章实践项目

13.6.1 PDF偏执狂

import os,PyPDF2,sys

os.chdir(r'C:\Users\Yuno\Desktop')
for foldername,subfoldernames,filenames in os.walk('.'): #遍历当前工作目录找到pdf文件
    for filename in filenames:
        if filename.endswith('.pdf'):
            try:
                
                pdfReader = PyPDF2.PdfFileReader\
                            (open(os.path.join\
                                  (foldername,filename),'rb'))         #为PDF创建的PdfFileReader对象                           
                                
                pdfWriter = PyPDF2.PdfFileWriter()
                for i in range(pdfReader.numPages):                #遍历拷贝到PdfFileWriter对象                        
                    pdfWriter.addPage(pdfReader.getPage(i))
                    pdfWriter.encrypt(sys.arge[1,])       #加密            
                    pdfFile = open\
                              (filename.replace\
                                   ('.pdf', '_encrypted.pdf'),'wb')                                                                                 
                    pdfWriter.write(pdfFile)
                    pdfFile.close()
                print('seccessfully encrypt file %s' %(filename))
            except:
                continue

            
print('Done')   

13.6.2 定制邀请函,保存为Word文档

from docx import Document

guestsFile = open('guests.txt')
nameList = guestsFile.readlines() #将每行名字生成列表
inviDoc = Document('INVITATION.docx')

for name in nameList:
    name=name.strip('\n')   #去掉换行符
#为docx文件添加内容
    inviDoc.add_paragraph('It would be a pleasure to have the company of',\
                          style='sample1')
    inviDoc.add_paragraph(name,style='NameSample')
    inviDoc.add_paragraph('At 11010 memory at the evenig of',style='sample1')
    inviDoc.add_paragraph('April 1st',style='datesample1')
    inviDoc.add_paragraph("At 7 o'clock",style='sample1')
    inviDoc.add_page_break()
inviDoc.save('AllInvitation.docx')
print('Done')

13.6.3暴力PDF口令破解程序

import PyPDF2,time
#记录开始时间
startTime = time.time()

pdfReader = PyPDF2.PdfFileReader(open('C:/Users/Yuno/Desktop/encrypted.pdf','rb'))
dicList = open('C:/Users/Yuno/Desktop/dictionary.txt').readlines()
print('starting program...')#创建PdfFileReader对象


for word in dicList:
    word = word.strip('\n')
    global num #num变量声称全局
    num = pdfReader.decrypt(word)
    print('Entering word :%s...' % (word), end='')
    print('\b'*len(word + 'Entering word :...'), end='',flush=True)
    if num == 1:
        print('The password is %s.' % (word))    
        break
    word = word.lower() #小写
    num = pdfReader.decrypt(word)
    print('Entering word :%s...' % (word), end='')
    print('\b'*len(word+ 'Entering word :...'), end='', flush=True)
    if num == 1:
        print('The password is :%s.' % (word))
        break
    
if num == 0:
    print('Password not found.')
totalTime = round((time.time() - startTime)/60) #计算时间
print('Took %s minutes to calculate' % (totalTime))
            

总结:因为等待需要十几分钟(不知是我的电脑问题还是啥...= =),所以做了一个当前单词的输出。

你可能感兴趣的:(《Python编程快速上手—让繁琐工作自动化》第13章实践项目)