title: Use Tesseract OCR to extract text from a scanned pdf folders
body: <p>I have the code to extract/convert text from scanned pdf files/normal pdf files by using Tesseract OCR. But I want to make my code to convert a pdf folder rather than a single pdf file, then the extract text files will be store in a folder that I want.</p>
  <p>See my code below:</p>
  <pre><code>filePath = '/Users/CodingStark/scanned/scanned-file.pdf'
  pages = convert_from_path(filePath, 500)
  
  
  image_counter = 1
    
  # Iterate through all the pages stored above 
  for page in pages: 
    
      filename = &quot;page_&quot;+str(image_counter)+&quot;.jpg&quot;
            
      page.save(filename, 'JPEG') 
    
      image_counter = image_counter + 1
      
  
  filelimit = image_counter-1
    
  # Creating a text file to write the output 
  outfile = &quot;scanned-file.txt&quot;
    
  
  f = open(outfile, &quot;a&quot;) 
    
  # Iterate from 1 to total number of pages 
  for i in range(1, filelimit + 1): 
  
      filename = &quot;page_&quot;+str(i)+&quot;.jpg&quot;
            
      # Recognize the text as string in image using pytesserct 
      text = str(((pytesseract.image_to_string(Image.open(filename))))) 
  
      text = text.replace('-\n', '')     
    
  
      f.write(text) 
  #Close the file after writing all the text. 
  f.close() 
  </code></pre>
  <p>I want to automate my code so it will convert all my pdf files in the scanned folder and those extract text files will be in a folder that I want. Also, are there any ways to delete all the jpg files after the code? Since it takes a lot of memory spaces. Thank you so much!!</p>
  <h1>Updated with Answer</h1>
  <pre><code>def tesseractOCR_pdf(pdf):
  
      filePath = pdf
      
      pages = convert_from_path(filePath, 500)
  
      # Counter to store images of each page of PDF to image 
      image_counter = 1
  
      # Iterate through all the pages stored above 
      for page in pages:
          # Declaring filename for each page of PDF as JPG 
          # For each page, filename will be: 
          # PDF page 1 -&gt; page_1.jpg 
          # PDF page 2 -&gt; page_2.jpg 
          # PDF page 3 -&gt; page_3.jpg 
          # .... 
          # PDF page n -&gt; page_n.jpg 
  
          filename = &quot;page_&quot;+str(image_counter)+&quot;.jpg&quot;
          
          # Save the image of the page in system 
          page.save(filename, 'JPEG') 
          # Increment the counter to update filename 
          image_counter = image_counter + 1
  
      # Variable to get count of total number of pages 
      filelimit = image_counter-1
  
  
      # Create an empty string for stroing purposes
      text = &quot;&quot;
      # Iterate from 1 to total number of pages 
      for i in range(1, filelimit + 1): 
          # Set filename to recognize text from 
          # Again, these files will be: 
          # page_1.jpg 
          # page_2.jpg 
          # .... 
          # page_n.jpg 
          filename = &quot;page_&quot;+str(i)+&quot;.jpg&quot;
  
          # Recognize the text as string in image using pytesserct 
          text += str(((pytesseract.image_to_string(Image.open(filename))))) 
  
          text = text.replace('-\n', '')     
  
      
      #Delete all the jpg files that created from above
      for i in glob.glob(&quot;*.jpg&quot;):
          os.remove(i)
          
      return text
  
  def tesseractOCR_img(img):
  
      filePath = img
      
      text = str(pytesseract.image_to_string(filePath,lang='eng',config='--psm 6'))
      
      text = text.replace('-\n', '')
      
      return text
  
  def Tesseract_ALL(docDir, txtDir):
      if docDir == &quot;&quot;: docDir = os.getcwd() + &quot;\\&quot; #if no docDir passed in 
          
      for doc in os.listdir(docDir): #iterate through docs in doc directory
          try:
              fileExtension = doc.split(&quot;.&quot;)[-1]
              
              if fileExtension == &quot;pdf&quot;:
                  pdfFilename = docDir + doc 
                  text = tesseractOCR_pdf(pdfFilename) #get string of text content of pdf
                  textFilename = txtDir + doc + &quot;.txt&quot;
                  textFile = open(textFilename, &quot;w&quot;) #make text file
                  textFile.write(text) #write text to text file
              else:   
  #             elif (fileExtension == &quot;tif&quot;) | (fileExtension == &quot;tiff&quot;) | (fileExtension == &quot;jpg&quot;):
                  imgFilename = docDir + doc 
                  text = tesseractOCR_img(imgFilename) #get string of text content of img
                  textFilename = txtDir + doc + &quot;.txt&quot;
                  textFile = open(textFilename, &quot;w&quot;) #make text file
                  textFile.write(text) #write text to text file
          except:
              print(&quot;Error in file: &quot;+ str(doc))
              
      for filename in os.listdir(txtDir):
          fileExtension = filename.split(&quot;.&quot;)[-2]
          if fileExtension == &quot;pdf&quot;:
              os.rename(txtDir + filename, txtDir + filename.replace('.pdf', ''))
          elif fileExtension == &quot;tif&quot;:
              os.rename(txtDir + filename, txtDir + filename.replace('.tif', ''))
          elif fileExtension == &quot;tiff&quot;:
              os.rename(txtDir + filename, txtDir + filename.replace('.tiff', ''))
          elif fileExtension == &quot;jpg&quot;:
              os.rename(txtDir + filename, txtDir + filename.replace('.jpg', ''))
  
  #Below are the code to run the functions
  #Specific telling the function where the documents located and where you want the txt files to be at
  docDir = &quot;pdf_folder&quot;
  txtDir = &quot;text_folder&quot;
  
  Tesseract_ALL(docDir, txtDir)
  </code></pre>