1

I am using python 3. My code uses pdfminer to convert pdf to text. I want to get the output of these files in a new folder. Currently it's coming in the existing folder from which it does the conversion to .txt using pdfminer. How do I redirect the output to a different folder. I want the output in a folder called "D:\extracted_text" Code till now:

from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage
from io import StringIO
import glob
import os

def convert(fname, pages=None):
   if not pages:
       pagenums = set()
   else:
       pagenums = set(pages)

   output = StringIO()
   manager = PDFResourceManager()
   converter = TextConverter(manager, output, laparams=LAParams())
   interpreter = PDFPageInterpreter(manager, converter)

   infile = open(fname, 'rb')
   for page in PDFPage.get_pages(infile, pagenums):
       interpreter.process_page(page)
   infile.close()
   converter.close()
   text = output.getvalue()   
   output.close

   savepath = 'D:/extracted_text/'
   outfile = os.path.splitext(fname)[0] + '.txt'
   comp_name = os.path.join(savepath,outfile)
   print(outfile)
   with open(comp_name, 'w', encoding = 'utf-8') as pdf_file:
       pdf_file.write(text)

   return text    



directory = glob.glob(r'D:\files\*.pdf')  

for myfiles in directory:  
     convert(myfiles)
ajai biltu
  • 55
  • 6
  • So… what happens instead when you run it? – Davis Herring Jun 07 '19 at 23:35
  • The converted files are coming in the same directory as that of the pdf files – ajai biltu Jun 08 '19 at 19:34
  • Possible duplicate of [Why doesn't os.path.join() work in this case?](https://stackoverflow.com/questions/1945920/why-doesnt-os-path-join-work-in-this-case) – Davis Herring Jun 08 '19 at 19:36
  • Possible duplicate of [Redirect output of a function that converts pdf to txt files to a new folder in python](https://stackoverflow.com/questions/56482437/redirect-output-of-a-function-that-converts-pdf-to-txt-files-to-a-new-folder-in) . This is basically the third attempt to ask the same question, and it is not clear what research has been don in the interim. – Jonathan Leffler Jun 09 '19 at 05:11

0 Answers0