我已经成功安装了pyPDF,但是extractText方法效果不好,所以我决定试试pyPDF2,问题是,提取文本的时候出现异常:
Traceback (most recent call last):
File "C:\Users\Asus\Desktop\pfdtest.py", line 44, in <module>
test2()
File "C:\Users\Asus\Desktop\pfdtest.py", line 41, in test2
print(mypdf.getPage(0).extractText())
File "C:\Python32\lib\site-packages\PyPDF2\pdf.py", line 1701, in extractText
content = ContentStream(content, self.pdf)
File "C:\Python32\lib\site-packages\PyPDF2\pdf.py", line 1783, in __init__
stream = StringIO(stream.getData())
TypeError: initial_value must be str or None, not bytes
这是我的示例代码:
filename = "myfile.pdf"
f = open(filename,'rb')
mypdf = PdfFileReader(f)
print(f,mypdf,mypdf.getNumPages())
print(mypdf.getPage(0).extractText())
它正确地确定了 pdf 中的页数,但是在读取流时存在问题。