In777
In777

Reputation: 181

convert several files with pdfminer

I've found code online which allows to convert several pdf files to text files, using the pdfminer module in Python. I tried to expand the code for several pdf files which I've saved in a directory, but the code results in an error.

My code so far:

import nltk
import re
import glob

from cStringIO import StringIO
from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
from pdfminer.converter import TextConverter
from pdfminer.layout import LAParams
from pdfminer.pdfpage import PDFPage

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 = file(fname, 'rb')
   for page in PDFPage.get_pages(infile, pagenums):
       interpreter.process_page(page)
   infile.close()
   converter.close()
   text = output.getvalue()
   output.close

   with open('D:\Reports\*.txt', 'w') as pdf_file:
       pdf_file.write(text)

   return text

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

for myfiles in directory:  
     convert(myfiles)

The error message:

Traceback (most recent call last):
  File "F:/Text mining/pdfminer for several files", line 40, in <module>
    convert(myfiles)
  File "F:/Text mining/pdfminer for several files", line 32, in convert
    with open('D:\Reports\*.txt', 'w') as pdf_file:
IOError: [Errno 22] invalid mode ('w') or filename: 'D:\\Reports\\*.txt'

Upvotes: 0

Views: 1299

Answers (2)

Oliver W.
Oliver W.

Reputation: 13459

The error stems from attempting to write the contents of the text variable to a file that is named 'D:\Reports\*.txt'. The wildcard * is not allowed in a filename (ref).

If you want to save the file to a text file with the same name, you could replace your writing functionality with:

   outfile = os.path.splitext(os.path.abspath(fname))[0] + '.txt'
   with open(outfile, 'wb') as pdf_file:
       pdf_file.write(text)

Do not forget to import os if you want to process paths in an OS agnostic way.

Upvotes: 1

Markus Dutschke
Markus Dutschke

Reputation: 10606

probably you should just change:

with open('D:\Reports\*.txt', 'w') as pdf_file:
    pdf_file.write(text)

to

with open(fname, 'w') as pdf_file:
    pdf_file.write(text)

but I do not have python2.7-3.4 on my machine available to verify

Upvotes: 0

Related Questions