(博主亲自录制视频)
程序增加了智能判断时间功能,根据pdf文档页数来调整等待时间,页数越长,时间越长,反之亦然。
如果读取页数出错,说明pdf文档质量不好,则返回10秒等待时间
这样降低了错误率。
# -*- coding: utf-8 -*-"""Created on Thu May 12 11:22:57 2016excel 会自动保存,不用再自己保存提示:运行一次anaconda后要重新启动,anaconda和pyautogui有点不兼容其它软件突然弹出会扰乱程序,例如杀毒软件,搜狗,对话框等弹出pdf字符串超过8192个,也会出错,复制错位pdfpdf内容第一个字符是等号或减号时,会提示出错信息 #最新操作法:0.批量导入pdf文件名到excel内1.读取所有pdf文件名,保存到list_pdf_fileNames2.准备工作:打开pdf和excel3.循环把所有pad内容复制粘贴到excel内 3.1输入PDF文件名,并进入,全选内容,复制pdf内容 3.2返回桌面,打开excel 3.3pdf内容复制到cell内 3.4 保存excel,关闭excel4.保存excel,关闭excel;关闭pdf excel快捷键:窗口最大化:ctrl+F10进入相应cell:F2:切换:Alt+tab保存:ctrl+s关闭: alt+f4 pdf快捷键:打开pdf文档:ctrl+o全选:ctrl+a复制:ctrl+c粘贴:ctrl+v关闭pdf文档:ctrl+w关闭Adobe:ctrl+q @author: Administrator""" import pyautogui,time,os,PyPDF2,xlrd#任务栏锁定excel坐标(101,876) pdf坐标(174,875)office_dir_pdf_files="C:/Users/Administrator/Desktop/pdf批量提取到excel/guide_pdf/"home_dir_pdf_files="C:/Users/daxiong/Desktop/李佳pdf中文提取/guide_pdf/"dir="C:/Users/Administrator/Desktop/pdf批量提取到excel/guide_pdf/" data=xlrd.open_workbook("C:/Users/Administrator/Desktop/test1.xlsx")table=data.sheet_by_index(0)list_pdf_fileNames=table.col_values(0) def Get_time(i): filename=dir+list_pdf_fileNames[i] try: pdfFileObj=open(filename,'rb') pdfReader=PyPDF2.PdfFileReader(pdfFileObj) pages=pdfReader.numPages #显示页数 在第4100行时读取pdfReader也会出错 except: #print ("wrong when read pdf:",filename) sleepTime=10 return sleepTime if pages<=5: sleepTime=2 elif 5<=10: sleepTime=3 elif 10<=pages<20: sleepTime=5 elif 20<=pages<30: sleepTime=7 else: sleepTime=int(pages/6) return sleepTime #返回桌面,打开pdfdef Return_desktop_open_pdf(): #pyautogui.hotkey("winleft","d") #time.sleep(1) #打开存储PDF软件;(50,50)为pdf坐标 pyautogui.click(174,875) time.sleep(1) #返回桌面,打开excel def Return_desktop_open_excel(): #pyautogui.hotkey("winleft","d") #time.sleep(1) #打开存储excel软件;(55,189)为excel坐标 pyautogui.click(101,876) time.sleep(1) #双击安全参数5,特别是后期数据量变大 #保存,关闭exceldef Save_and_close_excel(): pyautogui.hotkey("ctrl","s") time.sleep(10) #安全参数5-10 #pyautogui.hotkey("alt","f4") #time.sleep(1) #安全参数2 #excel复制一个单元格内容def Excel_copy_oneCellContent(i): pyautogui.press("f2") time.sleep(1) #粘贴信息 pyautogui.hotkey("ctrl","v") sleeptime=Get_time(i) time.sleep(sleeptime) #安全参数10 pyautogui.press("enter") time.sleep(1) #pyautogui.hotkey("winleft","d") #time.sleep(1) def Copy_one_pdfToExcel(i): fileName=list_pdf_fileNames[i] #循环把所有pad内容复制粘贴到excel内 #打开存储PDF软件;(55,189)为pdf坐标 pyautogui.click(174,875) time.sleep(1) pyautogui.hotkey("ctrl","o") time.sleep(1) #2.1输入PDF文件名,并进入 pyautogui.typewrite(fileName) time.sleep(1) pyautogui.press("enter") time.sleep(1) #选中pdf全部内容 pyautogui.hotkey("ctrl","a") time.sleep(1) sleeptime=Get_time(i) #安全参数10 #复制所有内容,等待时间设置长一点 pyautogui.hotkey("ctrl","c") time.sleep(sleeptime) #关闭pdf文档 pyautogui.hotkey("ctrl","w") time.sleep(1) #安全参数2 #print("ok for test") #2.2返回桌面,打开excel Return_desktop_open_excel() #print("ok for test1") #excel复制一个单元格内容 Excel_copy_oneCellContent(i) #print("ok for test2") #准备工作:excel的cell默认锁定位置是(B,2),最大化 pyautogui.doubleClick(50,50)time.sleep(5) #安全参数5,特别是后期数据量变大pyautogui.hotkey("winleft","d")time.sleep(1)#循环把所有pad内容复制粘贴到excel内for i in range(len(list_pdf_fileNames)): Copy_one_pdfToExcel(i)#3.关闭Adobe acrobat9.0pyautogui.hotkey("ctrl","q")