title: How to extract PDF fields from a filled out form in Python?
body: <p>I'm trying to use Python to processes some PDF forms that were filled out and signed using Adobe Acrobat Reader.</p>
  <p>I've tried:</p>
  <ul>
  <li>The <a href="http://www.unixuser.org/%7Eeuske/python/pdfminer/" rel="noreferrer">pdfminer</a> demo: it didn't dump any of the filled out data.</li>
  <li><a href="http://pybrary.net/pyPdf/" rel="noreferrer">pyPdf</a>: it maxed a core for 2 minutes when I tried to load the file with PdfFileReader(f) and I just gave up and killed it.</li>
  <li>Jython and <a href="http://pdfbox.apache.org/" rel="noreferrer">PDFBox</a>: got that working great but the startup time is excessive, I'll just write an external utility in straight Java if that's my only option.</li>
  </ul>
  <p>I can keep hunting for libraries and trying them but I'm hoping someone already has an efficient solution for this.</p>
  <hr />
  <p><strong>Update:</strong> Based on Steven's answer I looked into pdfminer and it did the trick nicely.</p>
  <pre><code>from argparse import ArgumentParser
  import pickle
  import pprint
  from pdfminer.pdfparser import PDFParser
  from pdfminer.pdfdocument import PDFDocument
  from pdfminer.pdftypes import resolve1, PDFObjRef
  
  def load_form(filename):
      &quot;&quot;&quot;Load pdf form contents into a nested list of name/value tuples&quot;&quot;&quot;
      with open(filename, 'rb') as file:
          parser = PDFParser(file)
          doc = PDFDocument(parser)
          return [load_fields(resolve1(f)) for f in
                     resolve1(doc.catalog['AcroForm'])['Fields']]
  
  def load_fields(field):
      &quot;&quot;&quot;Recursively load form fields&quot;&quot;&quot;
      form = field.get('Kids', None)
      if form:
          return [load_fields(resolve1(f)) for f in form]
      else:
          # Some field types, like signatures, need extra resolving
          return (field.get('T').decode('utf-16'), resolve1(field.get('V')))
  
  def parse_cli():
      &quot;&quot;&quot;Load command line arguments&quot;&quot;&quot;
      parser = ArgumentParser(description='Dump the form contents of a PDF.')
      parser.add_argument('file', metavar='pdf_form',
                      help='PDF Form to dump the contents of')
      parser.add_argument('-o', '--out', help='Write output to file',
                        default=None, metavar='FILE')
      parser.add_argument('-p', '--pickle', action='store_true', default=False,
                        help='Format output for python consumption')
      return parser.parse_args()
  
  def main():
      args = parse_cli()
      form = load_form(args.file)
      if args.out:
          with open(args.out, 'w') as outfile:
              if args.pickle:
                  pickle.dump(form, outfile)
              else:
                  pp = pprint.PrettyPrinter(indent=2)
                  file.write(pp.pformat(form))
      else:
          if args.pickle:
              print(pickle.dumps(form))
          else:
              pp = pprint.PrettyPrinter(indent=2)
              pp.pprint(form)
  
  if __name__ == '__main__':
      main()
  </code></pre>