Python滴滴行程单PDF文档数据并导出到Excel_第1页
Python滴滴行程单PDF文档数据并导出到Excel_第2页
Python滴滴行程单PDF文档数据并导出到Excel_第3页
全文预览已结束

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

Python整理滴滴行程单PDF文档数据并导出到Excel##Usage###安装依赖```pipinstallXlsxWriterpipinstallpdfminer3k```###运行```python3pdf2xlsx.py#--path(可选参数,pdf文件路径,默认为当前目录)..7.8.9.#-*-encoding:utf8-*-"""date:2022/3/24"""importos,sysimportreimporttimeimportxlsxwriterimportloggingfrompdfminer.pdfparserimportPDFParser,PDFDocumentfrompdfminer.pdfinterpimportPDFResourceManager,PDFPageInterpreterfrompdfminer.converterimportPDFPageAggregatorfrompdfminer.layoutimportLAParams,LTTextBox,LTTextLpagate=Falselogging.getLogger().setLevel(logging.ERROR)#pdf文档0.defpdf_parser(input_path):withopen(input_path,'rb')asfd:21.parse=PDFParser(fd)22.doc=PDFDocument()23.parse.set_document(doc)24.doc.set_parser(parse)25.doc.initialize('')26.resmgr=PDFResourceManager()27.laparams=LAParams()28.laparams.char_margin=1.029.laparams.word_margin=1.030.device=PDFPageAggregator(resmgr,laparams=laparams)31.interpreter=PDFPageInterpreter(resmgr,device)32. extracted_text=''33.34.forpageindoc.get_pages():cess_page(page)layouts=device.get_result()forlayoutinlayouts:ifisinstance(layout,LTTextBox)orisinstance(layout,LTTextLine):extracted_text+=layout.get_text()returnextracted_text#匹配有用的信息写入字典当中defextracted_info(extracted_text,name):tmp_list=[]t_list=total_text=count=total_text)[0]total=total_text)[0]51. tt=re.findall("(\d{2})-(\d{2})(\d{2}):(\d{2})([\u4e00-\u9fa5]{2})",extracted_text#行车时间 dd=深圳市\\n(.*)\\n",extracted_text)#行车起点fare=re.findall("(\d{2}\\.\d{2})",extracted_text)车费fori,lstinenumerate(tt):tmp_dict={}行程人"]=name上车时间"]=format_time(time_text=tt[i])上车地点"]=dd[i]车费"]=tmp_list.append(tmp_dict)returntmp_list,count,total#设置好表格格式后将字典的数据写入Excel当中defdict_to_excel(output_path,pdffilename_list,header):excel_init_file=xlsxwriter.Workbook(output_path)table=excel_init_file.add_worksheet('sheet1') title_format=True,'border':2,'bg_color':'#595959','font_color':'white','font_size':12,'font':'黑体','align':'center','valign':'vcenter'}) cell_format=1,'align':'center','valign':'vcenter'})table.set_default_row(40)70.71.fori,jinenumerate(header):72.table.set_column(i,i,30)73.i,j,title_format)74.last_idx=0fori,pdfinenumerate(pdffilename_list):正在识别:{}\n".format(pdf))#sys.stdout.write("%s[%-25s]%d%%生成Excel表格:",'#'*((i+1)*25//len(pdffilename_list)),(i+1)/len(pdffilename_list)*100))sys.stdout.flush()time.sleep(0.5) name=-(.*).pdf$',pdf)[0].strip()#行程人姓名text=pdf_parser(pdf)dict_list,count,total=extracted_info(text,name)forindex,dict_contentinenumerate(dict_list):foriinrange(len(header)-1): i,dict_content[header[i]],cell_format)ifint(count)!=1: table.merge_range('E{}:E{}'.format(str(last_idx+2),str(last_idx+2+int(count)-1)),'MergedRange',cell_format) table.merge_range('A{}:A{}'.format(str(last_idx+2),str(last_idx+2+int(count)-1)),'MergerdRange',cell_format)table.write_string(last_idx+1,0,name,cell_format)table.write_string(last_idx+1,4,total,cell_format)last_idx+=int(count)excel_init_file.close()#格式化乘车日期defformat_time(time_text):tt=list(time_text)tt.insert(1,'-')tt.insert(3,'')tt.insert(5,':')tt.insert(7,'')return''.join(tt)#pdf文件defget_all_pdf(path):108.return108.return[fforfinos.listdir(path)ifos.path.isfile(f)andf.endswith('.pdf')]109.#设置执行语句,开始进行转换if name ==' main 15.116.117.pdffilena

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

最新文档

评论

0/150

提交评论