You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用Haystack PDFToTextConverter转换本地PDF遇getText()参数错误求助

问题

运行Haystack入门教程一切正常,但尝试用本地PDF文件替代维基百科《权游》文章时持续报错。使用的代码如下:

from haystack.nodes import PDFToTextConverter
from pathlib import Path


def haystack():
    converter = PDFToTextConverter(
        remove_numeric_tables=True,
        valid_languages=["de"]
    )

    docs = converter.convert(file_path=Path("C:/Users/Franzi/Documents/myPDF.pdf"), meta=None)


if __name__ == '__main__':
    haystack()

报错信息:

File "C:\Users\Franzi\PycharmProjects\pythonProject2\main.py", line 15, in <module>
    haystack()
  File "C:\Users\Franzi\PycharmProjects\pythonProject2\main.py", line 11, in haystack
    docs = converter.convert(file_path=Path("C:/Users/Franzi/Documents/myPDF.pdf"), meta=None)
  File "C:\Users\Franzi\AppData\Local\Programs\Python\Python38\lib\site-packages\haystack\nodes\file_converter\pdf.py", line 171, in convert
    pages = self._read_pdf(
  File "C:\Users\Franzi\AppData\Local\Programs\Python\Python38\lib\site-packages\haystack\nodes\file_converter\pdf.py", line 301, in _read_pdf
    for page in results:
  File "C:\Users\Franzi\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\process.py", line 484, in _chain_from_iterable_of_lists
    for element in iterable:
  File "C:\Users\Franzi\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 611, in result_iterator
    yield fs.pop().result()
  File "C:\Users\Franzi\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 439, in result
    return self.__get_result()
  File "C:\Users\Franzi\AppData\Local\Programs\Python\Python38\lib\concurrent\futures\_base.py", line 388, in __get_result
    raise self._exception
TypeError: getText() got an unexpected keyword argument 'textpage'

环境为Python 3.8、PyCharm 2023.2,试过不同PDF文件,也尝试使用from haystack.utils import convert_files_to_docs; convert_files_to_docs(),仍出现相同错误。

解决方法

这个错误源于PyPDF2版本与Haystack不兼容。Haystack的PDFToTextConverter依赖特定版本的PyPDF2,新版本PyPDF2已移除textpage参数,但旧版Haystack代码仍在调用该参数。

  • 卸载当前PyPDF2版本:
    pip uninstall PyPDF2 -y
    
  • 安装兼容版本(推荐2.12.1,该版本适配Haystack的PDF转换器):
    pip install PyPDF2==2.12.1
    
  • 重新执行PDF转换代码即可解决问题。

另外,若使用Haystack v2.x版本,也可直接升级Haystack至最新稳定版,新版本已适配PyPDF2的更新。

内容的提问来源于stack exchange,提问作者geoidiot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.14 20:15:41