import re import pdfplumber def get_paytime(path): with pdfplumber.open(path) as pdf: page = pdf.pages[0] # 按照列表的规则,第四页 table = page.extract_text() time='' #华农商业 if '缴费时间: ' in table and ' 打印' in table: time="".join(re.findall(r'缴费时间: (.*?) 打印',table)) time=time[0]+time[2:] #华农交强 elif '收费确认时间:' in table and '有效保单生' in table: time = "".join(re.findall(r'收费确认时间:(.*?) 有效保单生', table)) time = time[0] + time[2:] #安盛 elif '收费确认时间' in table and '安盛天平' in table: time = "".join(re.findall(r'收费确认时间 (.*?)\n', table)) #众安 elif '收费确认时间' in table and '中国平安' in table: time = "".join(re.findall(r'收费确认时间:(.*?)时', table)) time=time.replace('年','-').replace('月','-').replace('日',' ') time=time.split('-') if len(time[1])==1: time[1]='0'+time[1] time="-".join(time)+':00' #人寿 elif '收付确认时间: ' in table and '中国人寿' in table: time = "".join(re.findall(r'收付确认时间: (.*?) 保单', table)) #太平商业 elif '保费确认时间:' in table and '太平财险' in table: time = "".join(re.findall(r'保费确认时间:(.*?) 确认码', table)) # 太平交强 elif '保费确认时间:' in table and '太平财产' in table: time = "".join(re.findall(r'保费确认时间:(.*?) 确认码', table)) # 恒邦商业 elif '保费确认时间:' in table and '恒邦' in table and '机动车交通事故责任强制保险' not in table: time = "".join(re.findall(r'保费确认时间:(.*?) 保单', table)) # 恒邦交强 elif '保费确认时间:' in table and '恒邦' in table and '机动车交通事故责任强制保险' in table: time = "".join(re.findall(r'保费确认时间:(.*?)\n', table)) return time