【问题标题】:PyPDF2 - PdfFileReader - cannot extract textPyPDF2 - PdfFileReader - 无法提取文本
【发布时间】:2021-02-06 01:39:45
【问题描述】:

我正在循环浏览一个目录并阅读大量 PDF。我正在使用循环从每个页面中提取所有文本信息。

5/13 PDF 在尝试使用 .getNumPages() 时抛出错误:发生异常:ValueError 无效的 int() 字面量为 10:b''。我认为发生此错误是因为对象(PyPDF2)显示 numPages: 0。

当前代码

dir = os.listdir(directory)

for f in dir:
object = PyPDF2.PdfFileReader(directory + '\\' + f)

NumPages = object.getNumPages()
text_output = ""  # Initiate Variable

# Loop through all pages and extract/merge text
with open(directory + '\\' + f, mode='rb') as FileName:
    reader = PyPDF2.PdfFileReader(FileName)
    for p_num in range(0, NumPages):
        page = reader.getPage(p_num)
        text_output = text_output + '\n' + 'PAGE: ' + \
            str(p_num + 1) + '\n' + page.extractText()

I added an image showing the object data where numPages: 0

我不明白为什么只有某些 PDF 存在这个问题。任何帮助将不胜感激!

【问题讨论】:

  • pdf 文件可能在某种程度上与普通 pdf 文件不同。您可以尝试在查看器中打开它们并再次将它们另存为 pdf 以尝试修复偏差。
  • 我试过这个没有成功。
  • @alexlong,你能分享你的 pdf 吗?

标签: python text-extraction pypdf2


【解决方案1】:

我测试了几个 pdf 库,我注意到 PyMuPDF 最适合阅读 pdf 文件。

这里的代码示例:

import fitz

doc = fitz.open("file.pdf")

for page in doc:
    text = page.getText()
    print(text)

【讨论】:

  • 这是处理 pdf 的最佳库。
【解决方案2】:

好吧,我也遇到了与 PyPDF2 相同的问题,所以我使用了另一个名为 slate 的 python 库

  • 安装库

    pip install slate3k
    
  • 然后使用下面的代码

    import slate3k as slate
    
    with open(file.pdf, 'rb') as f:
      extracted_text = slate.PDF(f)
      print(extracted_text)
    

【讨论】:

    【解决方案3】:

    我使用pdfminer提取pdf。

    您可以参考示例代码。

    #pip install pdfminer.six
    import io
    
    from pdfminer.pdfinterp import PDFResourceManager, PDFPageInterpreter
    from pdfminer.converter import TextConverter
    from pdfminer.layout import LAParams
    from pdfminer.pdfpage import PDFPage
    
    
    def convert_pdf_to_txt(path):
        '''Convert pdf content from a file path to text
    
        :path the file path
        '''
        rsrcmgr = PDFResourceManager()
        codec = 'utf-8'
        laparams = LAParams()
    
        with io.StringIO() as retstr:
            with TextConverter(rsrcmgr, retstr, codec=codec,
                               laparams=laparams) as device:
                with open(path, 'rb') as fp:
                    interpreter = PDFPageInterpreter(rsrcmgr, device)
                    password = ""
                    maxpages = 0
                    caching = True
                    pagenos = set()
    
                    for page in PDFPage.get_pages(fp,
                                                  pagenos,
                                                  maxpages=maxpages,
                                                  password=password,
                                                  caching=caching,
                                                  check_extractable=True):
                        interpreter.process_page(page)
    
                    return retstr.getvalue()
    
    
    if __name__ == "__main__":
        print(convert_pdf_to_txt('test.pdf'))
    

    有关此盖子的更多信息。您可以参考下面的链接

    PDFminer

    如果有任何问题,请检查并回复我。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-05-05
      • 1970-01-01
      • 2019-03-16
      • 1970-01-01
      • 2014-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多