Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/video/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x PythonPdfMiner将pdf文件转换为一个字符串块,单词之间没有空格_Python 3.x_Pdfminer - Fatal编程技术网

Python 3.x PythonPdfMiner将pdf文件转换为一个字符串块,单词之间没有空格

Python 3.x PythonPdfMiner将pdf文件转换为一个字符串块,单词之间没有空格,python-3.x,pdfminer,Python 3.x,Pdfminer,我使用的代码主要取自DuckPuncher对本文的回答,用于将PDF转换为文本文件: def convert_pdf_to_txt(path): rsrcmgr = PDFResourceManager() retstr = StringIO() codec = 'utf-8' laparams = LAParams() device = TextConverter(rsrcmgr, retstr, codec=codec, laparams=lapara

我使用的代码主要取自DuckPuncher对本文的回答,用于将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()返回的内容几乎是一个字符串块,单词之间没有空格。例如,从下载以下pdf并应用convert_pdf_to_txt()函数后,我得到了一个文本文件,其中的单词不以空格分隔。文本文件的摘录如下所示

3来自ComputerVisionArea的电池组的预选方法,例如plane+p ARALLAX4方法用于3数据集结构计算。在本文中,我们提出了一种新的基于自适应滤波技术的本地定位注册技术。自适应滤波器已成功地用于1-D的系统识别

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

根据这一点,一些PDF将整个文本标记为figure,默认情况下,PDFMiner不会尝试对figure文本执行布局分析。要覆盖此行为,需要将all_texts参数设置为True

下面是一个基于的适用于我的示例

根据这一点,一些PDF将整个文本标记为figure,默认情况下,PDFMiner不会尝试对figure文本执行布局分析。要覆盖此行为,需要将all_texts参数设置为True

下面是一个基于的适用于我的示例


您提供的链接已断开(并非完全为蓝色),并导致出现非PDF页面。你能提供你感兴趣的PDF示例的链接吗?@pyano是的,很抱歉链接断了。我已经编辑了帖子中的链接。现在应该可以了。谢谢你的帮助!您提供的链接已断开(并非完全为蓝色),并导致出现非PDF页面。你能提供你感兴趣的PDF示例的链接吗?@pyano是的,很抱歉链接断了。我已经编辑了帖子中的链接。现在应该可以了。谢谢你的帮助!
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')