#NEWLINE# def extract_pdf( path , languages = None ) : #NEWLINE# #INDENT# "\n    Extract content from a PDF file.\n\n    This will attempt to use pdfminer to extract textual content from\n    each page. If none is found, it'll send the images through OCR.\n    " #NEWLINE# with open ( path , 'rb' ) as fh : #NEWLINE# #INDENT# rsrcmgr = PDFResourceManager ( ) #NEWLINE# laparams = LAParams ( ) #NEWLINE# device = PDFPageAggregator ( rsrcmgr , laparams = laparams ) #NEWLINE# interpreter = PDFPageInterpreter ( rsrcmgr , device ) #NEWLINE# parser = PDFParser ( fh ) #NEWLINE# try : #NEWLINE# #INDENT# doc = PDFDocument ( v , '' ) #NEWLINE# #UNINDENT# except PDFSyntaxError as pse : #NEWLINE# #INDENT# if ( 'No /Root object!' in pse . message ) : #NEWLINE# #INDENT# log . info ( 'Invalid PDF file: %r' , path ) #NEWLINE# return None #NEWLINE# #UNINDENT# raise #NEWLINE# #UNINDENT# result = { #NEWLINE# #INDENT# 'pages' : [ ] , #NEWLINE# #UNINDENT# } #NEWLINE# if len ( doc . info ) : #NEWLINE# #INDENT# for ( k , v ) in doc . info [ ( - 1 ) ] . items ( ) : #NEWLINE# #INDENT# k = k . lower ( ) . strip ( ) #NEWLINE# if ( k != 'pages' ) : #NEWLINE# #INDENT# result [ k ] = safe_text ( v ) #NEWLINE# #UNINDENT# #UNINDENT# #UNINDENT# if ( not doc . is_extractable ) : #NEWLINE# #INDENT# log . warning ( 'PDF not extractable: %s' , path ) #NEWLINE# return result #NEWLINE# #UNINDENT# for ( i , page ) in enumerate ( PDFPage . create_pages ( doc ) ) : #NEWLINE# #INDENT# text = None #NEWLINE# try : #NEWLINE# #INDENT# interpreter . process_page ( page ) #NEWLINE# layout = device . get_result ( ) #NEWLINE# text = _convert_page ( layout , path , ( i + 1 ) , languages ) #NEWLINE# #UNINDENT# except Exception as ex : #NEWLINE# #INDENT# log . warning ( 'Failed to parse PDF page: %r' , ex ) #NEWLINE# #UNINDENT# text = ( text or '' ) #NEWLINE# result [ 'pages' ] . append ( text ) #NEWLINE# #UNINDENT# device . close ( ) #NEWLINE# return result