python - 使用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)
你知道一个可能对我有帮助的功能吗?谢谢!
解决方案
假设列是分开的," "
那么您可以使用该draw_rects()
函数从列中制作文本。
该过程要求您检测chars
并spaces
首先使用
im.reset().draw_rects(p0.chars)
然后使用
text = p0.extract_text()
将为您提供列的文本格式。此示例说明了如何完成 -从 PDF 中提取列文本
推荐阅读
- python - 基于另一个值“状态”生成 json 对象的变体、内部值
- java - 在 CaretListener 中获取变量(如 getActionCommand)
- css - 编辑 userChrome.css 以隐藏 tabstoolbar 和 nav-bar
- json - PHP的explode()函数的JSON中是否有等价物?
- typescript - 需要只读修饰符
- json - 如何使用 jq 选择具有特定值的字段
- c# - 所有项目的一个解决方案中的 C# 和引用
- django - 如何通过cms页面类型过滤获取所有页面详细信息?
- javascript - Eslint:如何处理依赖函数的 no-use-before-define?
- c# - ASP.Net Core 多币种应用