我不理解这个错误。我想打开一个pdf并循环浏览页面,但我遇到了这个异常,我通过谷歌搜索找不到太多。
这是失败的例子
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")
这是回溯
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
我有 python 3.6
在执行此操作之前,我将像这样保存 pdf 文件,因为我将内容保存在 base64 编码字符串中
decoded = base64.b64decode(content_string)
with open(tmpfiles_path+'tmpfile.pdf', 'wb') as fout:
fout.write(decoded)
可能是文件在保存时受到了一些保护吗?