python3.x:pdfminer3k Online, local parsing PDF

Source: Internet
Author: User

PYTHON3.X:PDFMINER3K online, local resolution PDF installation
Pip Install pdfminer3k
Example: Online parsing PDF
" "Demo:pdf2htmlex parsing pdf datetime:2018-02-22 15:30:00" "ImportimportlibImportSYSImportRandom fromUrllib.requestImportUrlopen fromUrllib.requestImportRequest fromPdfminer.converterImportPdfpageaggregator fromPdfminer.layoutImportLttextboxhorizontal, Laparams fromPdfminer.pdfinterpImportPdfresourcemanager, Pdfpageinterpreter fromPdfminer.pdfinterpImportpdftextextractionnotallowed fromPdfminer.pdfparserImportPdfparser, Pdfdocument" "Parse PDF text and save to TXT file" "importlib.reload (SYS) User_agent= ['mozilla/5.0 (Windows NT 10.0; WOW64)','mozilla/5.0 (Windows NT 6.3; WOW64)',              'mozilla/5.0 (Windows NT 6.1; WOW64; rv:54.0) gecko/20100101 firefox/54.0',              'mozilla/5.0 (Windows NT 6.1) applewebkit/537.11 (khtml, like Gecko) chrome/23.0.1271.64 safari/537.11',              'mozilla/5.0 (Windows NT 6.3; WOW64; trident/7.0; rv:11.0) Like Gecko',              'mozilla/5.0 (Windows NT 5.1) applewebkit/537.36 (khtml, like Gecko) chrome/28.0.1500.95 safari/537.36',              'mozilla/5.0 (Windows NT 6.1; WOW64; trident/7.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0;. net4.0c; rv:11.0) like Gecko)',              'mozilla/5.0 (Windows; U Windows NT 5.2) gecko/2008070208 firefox/3.0.1',              'mozilla/5.0 (Windows; U Windows NT 5.1) gecko/20070309 firefox/2.0.0.3',              'mozilla/5.0 (Windows; U Windows NT 5.1) gecko/20070803 firefox/1.5.0.12',              'opera/9.27 (Windows NT 5.2; U ZH-CN)',              'mozilla/5.0 (Macintosh; PPC Mac OS X; U EN) Opera 8.0',              'opera/8.0 (Macintosh; PPC Mac OS X; U EN)',              'mozilla/5.0 (Windows; U Windows NT 5.1; En-us; rv:1.8.1.12) gecko/20080219 firefox/2.0.0.12 navigator/9.0.0.6',              'mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; Win64; x64; trident/4.0)',              'mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; trident/4.0)',              'mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; trident/6.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0; infopath.2;. net4.0c;. NET4.0E)',              'mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.1 (khtml, like Gecko) maxthon/4.0.6.2000 chrome/26.0.1410.43 safari/537.1',              'mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; trident/6.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0; infopath.2;. net4.0c;. net4.0e; qqbrowser/7.3.9825.400)',              'mozilla/5.0 (Windows NT 6.1; WOW64; rv:21.0) gecko/20100101 firefox/21.0',              'mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/537.1 (khtml, like Gecko) chrome/21.0.1180.92 safari/537.1 lbbrowser',              'mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.1; WOW64; trident/6.0; Bidubrowser 2.x)',              'mozilla/5.0 (Windows NT 6.1; WOW64) applewebkit/536.11 (khtml, like Gecko) chrome/20.0.1132.11 taobrowser/3.0 safari/536.11']defParse (_path):#fp = open (_path, ' RB ') # RB opens a local PDF file in binary read modeRequest = Request (Url=_path, headers={'user-agent': Random.choice (User_agent)})#randomly extracting an element from a user_agent listfp = Urlopen (Request)#Open an online PDF document    #using a File object to create a PDF document ParserPraser_pdf =pdfparser (FP)#Create a PDF documentDoc =pdfdocument ()#Connection Analyzer and Document Objectpraser_pdf.set_document (Doc) Doc.set_parser (praser_pdf)#provide initialization password doc.initialize ("123456")    #Create an empty string if you don't have a passworddoc.initialize ()#detects if the document provides a TXT conversion, does not provide a ignore    if  notdoc.is_extractable:RaisepdftextextractionnotallowedElse:        #Create a PDF Explorer to manage shared resourcesRsrcmgr =Pdfresourcemanager ()#Create a PDF parameter AnalyzerLaparams =Laparams ()#Create aggregatordevice = Pdfpageaggregator (Rsrcmgr, laparams=laparams)#Create a PDF page interpreter objectInterpreter =pdfpageinterpreter (rsrcmgr, device)#iterate through the list, processing one page at a time        #doc.get_pages () get page list         forPageinchdoc.get_pages ():#use the page interpreter to readinterpreter.process_page (page)#get content using aggregatorLayout =Device.get_result ()#here layout is a Ltpage object that contains the page parse out the various objects generally include Lttextbox, Ltfigure, Ltimage, lttextboxhorizontal and so on To get the text, you get the object's Text property,             forOutinchlayout:#determine if there is a Get_text () method, pictures, etc.                #if Hasattr (out, "Get_text"):                ifisinstance (out, lttextboxhorizontal): Results=Out.get_text ()Print("Results:"+results)if __name__=='__main__': URL="http://www.sac.net.cn/hysj/zqgsyjpm/201707/P020170717564197883913.pdf"Parse (URL)

Note: pdfminer3k support for tabular data is not very friendly, you can use PDF2HTMLWX (non-Python third party libraries) to convert the PDF to HTML, and then parse the HTML;

python3.x:pdfminer3k Online, local parsing PDF

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.