title: Reading line in PDF using python
body: <p>I am trying write a program which give trace matrix for a requirement -code- Test</p>
  
  <p>for which i am not able to read the requirement tags from PDF line by line from PDF. </p>
  
  <p>Below is the program i tried.</p>
  
  <pre><code>import os, sys, time
  import sys
  import glob
  import xlwt
  sys.path.insert(0,'C:/Python27/xlwt-0.7.5')
  import pyPdf
  from StringIO import StringIO
  import docx
  
  req_path_py = os.path.dirname(os.path.abspath(__file__)) +"\\Requirement\\"
  req_list = glob.glob("%s/*.pdf" %req_path_py)
  
  def getPDFContent(path):
      content = ""
      # Load PDF into pyPDF
      pdf = pyPdf.PdfFileReader(file(path, "rb"))
      # Iterate pages
      for i in range(0, pdf.getNumPages()):
          # Extract text from page and add to content
          content += pdf.getPage(i).extractText() + "\n"
      # Collapse whitespace
      content = " ".join(content.replace(u"\xa0", " ").strip().split())
      return content
  def topReq():
      global req_path_py, req_list
      with open("traceMetrix.txt","w") as txt:
          txt.write("CSD ID \tSRD ID \tSDD ID\tCODE ID\tTEST ID\n")
          pdfContent = StringIO(getPDFContent(req_list[0]).encode("ascii", "ignore"))
          for line in pdfContent:
              if '{CSD' in line:
                  txt.write(line)
  
  if __name__ == "__main__":
      topReq()
  </code></pre>
  
  <p>Content of PDF looks like below</p>
  
  <pre><code>Trace:
  {CSD-PROS-PLN-001}
  Abc shall do abc
  Trace:
  {CSD-PROS-PLN-002}
  Abc shall do abc
  Trace:
  {CSD-PROS-PLN-003}
  Abc shall do abc
  </code></pre>
  
  <p>Also sometimes some content will be inside table sy below entry is inside a table</p>
  
  <pre><code>Trace:
  {CSD-PROS-PLN-003}
  Abc shall do abc
  </code></pre>
  
  <p>The purpose of such search is say, i get top level req as "{CSD-PROS-PLN-003}" then i will search other pdf / code/test for the derived requirement / implementation / test from this requirement.</p>
  
  <p>Thanks</p>