title: How can I extract text from different parts of a pdf and create an excel?
body: <p>I'm working with thousands of pdfs that I want to read with python and extract the necessary information.</p>
  <p>In order to read the pdf I used this code:</p>
  <pre><code>import xlsxwriter
  from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
  from pdfminer.converter import TextConverter
  from pdfminer.layout import LAParams
  from pdfminer.pdfpage import PDFPage
  from six import StringIO
  
  def convert_pdf_to_txt(path):
      rsrcmgr = PDFResourceManager()
      retstr = StringIO()
      laparams = LAParams()
      device = TextConverter(rsrcmgr, retstr, laparams=laparams)
      fp = open(path, 'rb')
      interpreter = PDFPageInterpreter(rsrcmgr, device)
      password = &quot;&quot;
      maxpages = 0
      caching = True
      pagenos=set()
      for page in PDFPage.get_pages(fp, pagenos, maxpages=maxpages, password=password,caching=caching, check_extractable=True):
          interpreter.process_page(page)
      fp.close()
      device.close()
      str = retstr.getvalue()
      retstr.close()
  
      return str.replace(&quot;\\n&quot;,&quot;\n&quot;)
  </code></pre>
  <p>The output of this function is the next one:</p>
  <pre><code>Subject ID
  15006-1308
  
  Report Date
  16 Jul 2020
  
  Sample Type Slide Deck
  Site Lymph Node
  
  GENE
  PTEN
  PTEN
  GNAS
  TP53
  
  ALTERATION
  loss
  A328fs*16
  R160C
  T253N
  </code></pre>
  <p>The first part of the information can be stored with conditionals. For example:</p>
  <pre><code>lines = list(filter(bool,string.split('\n')))
  #print(lines)
      
      custData = {}
      for i in range(len(lines)):
          if 'Subject ID' in lines[i]:
              custData['Subjet'] = lines[i+1]
          elif 'Report Date' in lines[i]:
              custData['Report_Date'] = lines[i+1]
  </code></pre>
  <p>Then I can use pandas to make a dataframe:</p>
  <pre><code>df = pd.DataFrame(data=[custData])
  
  df.to_excel (r'export_dataframe.xlsx', index = True, header=True)
  </code></pre>
  <p>My question is about the GENE and ALTERATION part of the text.<br />
  I wish to have this in the excel file, but I don't know how to do it because first the genes are shown and then the alterations.
  What I would like to have in the excel file is something like this.</p>
  <pre><code>     PTEN      | GNAS   |    TP53
  loss;A328fs*16 | R160C  |    T253N
  </code></pre>
  <p>Thank you in advance.</p>