#NEWLINE# def convert_pdf( filename , type = 'xml' ) : #NEWLINE# #INDENT# commands = { #NEWLINE# #INDENT# 'text' : [ 'pdftotext' , '-layout' , filename , '-' ] , #NEWLINE# 'text-nolayout' : [ 'pdftotext' , commands , '-' ] , #NEWLINE# 'xml' : [ 'pdftohtml' , '-xml' , '-stdout' , filename ] , #NEWLINE# 'html' : [ 'pdftohtml' , '-stdout' , filename ] , #NEWLINE# #UNINDENT# } #NEWLINE# try : #NEWLINE# #INDENT# pipe = subprocess . Popen ( commands [ type ] , stdout = subprocess . PIPE , close_fds = True ) . stdout #NEWLINE# #UNINDENT# except OSError as e : #NEWLINE# #INDENT# raise EnvironmentError ( ( 'error running %s, missing executable? [%s]' % ' ' . join ( commands [ type ] ) ) , e ) #NEWLINE# #UNINDENT# data = pipe . read ( ) #NEWLINE# pipe . close ( ) #NEWLINE# return data