I'm not understanding this error. I want to open a pdf and loop over the pages but I'm getting this exception and I couldn't find much by googling it.
Here is the example that fails
from pdfminer.pdfparser import PDFParser, PDFDocument
from os.path import basename, splitext
file = 'tmpfiles/tmpfile.pdf'
filename = splitext(basename(file))[0]
fp = open(file, 'rb')
parser = PDFParser(fp)
doc = PDFDocument(parser)
num_page = 0
text = ""
pages = doc.get_pages()
for p in pages:
print("do whatever")
Here is the traceback
Traceback (most recent call last):
File "test.py", line 20, in <module>
for p in pages:
File "/home/.../anaconda3/lib/python3.6/site-packages/pdfminer/pdfparser.py", line 544, in get_pages
raise PDFException('PDFDocument is not initialized')
pdfminer.pdftypes.PDFException: PDFDocument is not initialized
I have python 3.6
Before doing this I'm saving the pdf file like this because I have the contents in a base64 encoded string
decoded = base64.b64decode(content_string)
with open(tmpfiles_path+'tmpfile.pdf', 'wb') as fout:
fout.write(decoded)
Could it be that the file is being saved with some protection?
The problem was the version of pdfminer I was using. By installing pdfminer.six and changing the code in this way
from pdfminer.pdfpage import PDFPage
file = 'tmpfiles/tmpfile.pdf'
fp = open(file, 'rb')
pages = PDFPage.get_pages(fp)
for p in pages:
print("do whatever")
Now it works.