首页 文章

尝试提取文本时pyPDF2 TypeError

提问于
浏览
2

我已经成功安装了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中的页面数量,但是在读取流时存在问题 .

1 回答

  • 1

    这是与PyPDF2和Python 3中的兼容性相关的问题 .

    在我的情况下,我已经通过将 pdf.pyutils.py 替换为您将找到的here来解决它,如果您正在运行Python 3,它们基本上可以控制它,如果您是,则接收数据作为字节而不是字符串 .

相关问题