python-3.x - python pdfminer 将pdf文件转换为一大块字符串,单词之间没有空格

标签 python-3.x pdfminer

我使用的以下代码主要取自 DuckPuncher 对这篇文章的回答 Extracting text from a PDF file using PDFMiner in python?将 pdf 转换为文本文件:

def convert_pdf_to_txt(path):
    rsrcmgr = PDFResourceManager()
    retstr = StringIO()
    codec = 'utf-8'
    laparams = LAParams()
    device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=laparams)
    fp = open(path, 'rb')
    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)
    fp.close()
    device.close()
    str = retstr.getvalue()
    retstr.close()
    return str

使用以下代码下载 pdf 并将其存储在我的本地目录中,并存储在我的本地目录中。效果很好。

import requests
url = 'link_to_the_pdf'
file_name = './name.pdf'
response = requests.get(url)
with open(file_name, 'wb') as f:
    f.write(response.content)

但是,对于某些 pdf,convert_pdf_to_txt() 将内容返回为几乎一大块字符串,单词之间没有空格。例如,从http://www.ece.rochester.edu/~gsharma/papers/LocalImageRegisterEI2005.pdf下载以下pdf后,并应用 Convert_pdf_to_txt() 函数,我得到了一个文本文件,其中单词不以空格分隔。文本文件的摘录是

3Predominantmethodsinthelattergrouparefromcomputervisionarea,e.g.,plane+p arallax4methodfor3-Dscenestructurecomputation.Inthispaper,weproposeanewlocalimageregistrationtechnique,inthefirstclass,basedonadaptivefilteringtechniques.Adaptivefiltershavebeenutilizedsuccessfullyforsystemidentificationpurposesin1-D.

有人可以帮我解决这个问题吗?是这个特定 pdf 的格式导致了问题还是其他原因,因为对于其他一些 pdf,convert_pdf_to_txt() 函数工作正常。

最佳答案

根据这个thread有些 pdf 将整个文本标记为图形,并且默认情况下 PDFMiner 不会尝试对图形文本执行布局分析。要覆盖此行为,需要将 all_texts 参数设置为 True。

这是一个基于 this post 对我有用的示例.

import io

import pdfminer
from pdfminer.converter import TextConverter
from pdfminer.pdfinterp import PDFPageInterpreter
from pdfminer.pdfinterp import PDFResourceManager
from pdfminer.pdfpage import PDFPage

# Perform layout analysis for all text
laparams = pdfminer.layout.LAParams()
setattr(laparams, 'all_texts', True)

def extract_text_from_pdf(pdf_path):
    resource_manager = PDFResourceManager()
    fake_file_handle = io.StringIO()
    converter = TextConverter(resource_manager, fake_file_handle, laparams=laparams)
    page_interpreter = PDFPageInterpreter(resource_manager, converter)

    with open(pdf_path, 'rb') as fh:
        for page in PDFPage.get_pages(fh, 
                                      caching=True,
                                      check_extractable=True):
            page_interpreter.process_page(page)

        text = fake_file_handle.getvalue()

    # close open handles
    converter.close()
    fake_file_handle.close()

    if text:
        return text


text = extract_text_from_pdf('test.pdf')

关于python-3.x - python pdfminer 将pdf文件转换为一大块字符串,单词之间没有空格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55866078/

相关文章:

python - 如何在 Mac 上将 Python3 设置为默认的 python 版本?

python - 打印到屏幕时是否需要使用 "+"运算符连接字符串。 {使用版本 3.3.1}

python-3.x - 使用 python 应用 Wiener 滤波器去除噪声

python - 将 PDF 转换为文本 : "Text extraction is not allowed"

python - 使用 Python 解析 PDF 教科书中的索引页

python - 检查Python中连续相等元素的数量

python-3.x - 如何将可调用对象作为参数传递给 `functools.partial`

python - 按第一个目录级别批量拆分 PDF?

python - 是否可以在 pdfquery 中使用正则表达式?

python - 尝试使用 pdfminer.6 提取文本时如何修复 'UnicodeDecodeError'?