0

我在网上找到了允许使用pdfminerPython 中的模块将几个 pdf 文件转换为文本文件的代码。我试图扩展我保存在一个目录中的几个 pdf 文件的代码,但代码导致错误。

到目前为止我的代码:

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)

错误信息:

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'
4

2 回答 2

1

该错误源于试图将text变量的内容写入名为'D:\Reports\*.txt'. 文件名 ( ref )*中不允许使用通配符。

如果要将文件保存到具有相同名称的文本文件中,可以将写入功能替换为:

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

import os如果您想以与操作系统无关的方式处理路径,请不要忘记。

于 2016-11-28T17:47:59.440 回答
0

也许你应该改变:

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

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

但我的机器上没有可用于验证的 python2.7-3.4

于 2016-11-28T17:40:55.620 回答