PDFファイルをアップロードし、その過程でそれらを解析するためのスクリプトを書いています。解析にはPDFminerを使用します。
ファイルを PDFMiner ドキュメントに変換するには、次の関数を使用します。上記のリンクにある手順に従ってください。
def load_document(self, _file = None):
"""turn the file into a PDFMiner document"""
if _file == None:
_file = self.options['file']
parser = PDFParser(_file)
doc = PDFDocument()
doc.set_parser(parser)
if self.options['password']:
password = self.options['password']
else:
password = ""
doc.initialize(password)
if not doc.is_extractable:
raise ValueError("PDF text extraction not allowed")
return doc
もちろん、期待される結果は素晴らしいPDFDocument
インスタンスですが、代わりにエラーが発生します。
Traceback (most recent call last):
File "bzk_pdf.py", line 45, in <module>
cli.run_cli(BZKPDFScraper)
File "/home/toon/Projects/amcat/amcat/scripts/tools/cli.py", line 61, in run_cli
instance = cls(options)
File "/home/toon/Projects/amcat/amcat/scraping/pdf.py", line 44, in __init__
self.doc = self.load_document()
File "/home/toon/Projects/amcat/amcat/scraping/pdf.py", line 56, in load_document
doc.set_parser(parser)
File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdfparser.py", line 327, in set_parser
self.info.append(dict_value(trailer['Info']))
File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdftypes.py", line 132, in dict_value
x = resolve1(x)
File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdftypes.py", line 60, in resolve1
x = x.resolve()
File "/usr/local/lib/python2.7/dist-packages/pdfminer/pdftypes.py", line 49, in resolve
return self.doc.getobj(self.objid)
AttributeError: 'NoneType' object has no attribute 'getobj'
どこを見ればいいのかわからず、同じ問題を抱えている人は他にいません。
役立つかもしれないいくつかの追加情報:
- ここに私のテストファイルがあります: http://www.2shared.com/document/kM_wrI3J/testpdf.html
_file
django File objectですが、通常のファイルを使用しても同じ結果になります- pdfminer バージョン: 'pdfminer-20110515'
- Django: 1.4.3 (関係ないと思います)
- パイソン 2.7.3