首页 > 解决方案 > 使用pdfplumber(Python)从包含多列的pdf中提取文本

问题描述

我正在从 pdf(使用 python)中提取文本以分析它们,因此我正在大量处理科学论文。我正在使用pdfplumber,它运行良好,唯一的问题是此类 pdf 通常包含,我还没有找到一种方法让我的算法识别这一点。

我的代码是:

text = ""
with pdfplumber.open(r'example.pdf') as pdf:
    pages = pdf.pages
    for i, pg in enumerate(pages):
        text = text + " " + pages[i].extract_text(x_tolerance = 1)
text = text.replace('\n',' ')
text = text.replace('\r',' ')
text = text.replace('\no',' ')
text = text.replace('\nD',' ')
text = text.lower()
text = re.sub(r'[^a-zA-Z0-9\s]', ' ', text)

你知道一个可能对我有帮助的功能吗?谢谢!

标签: pythonmultiple-columnstext-extraction

解决方案


假设列是分开的," "那么您可以使用该draw_rects()函数从列中制作文本。

该过程要求您检测charsspaces首先使用

im.reset().draw_rects(p0.chars)

然后使用

text = p0.extract_text()

将为您提供列的文本格式。此示例说明了如何完成 -从 PDF 中提取列文本


推荐阅读