首页 > 解决方案 > Tabula 未检测到某些带有使用相同软件构建的 pdf 的表格

问题描述

我正在尝试在我的银行提供的信用卡报告中自动分类我的费用。

我发现 tabula 在许多 pdf 中都像奇迹一样工作,它非常容易转换为 csv,然后根据需要进行处理,只需两行代码: https ://tabula-py.readthedocs.io/en/latest/ tabula.html#high-level-interfaces

    from tabula import convert_into_by_batch

    convert_into_by_batch('movements', output_format='csv', pages='all')

但是我遇到了麻烦,因为在某些 pdf 中我根本没有得到结果,而且对我来说这看起来不合逻辑。因为:

  1. pdfs 显然看起来是一样的。由于显而易见的原因,我无法显示原始文件,但我可以分享两个模糊的屏幕截图,您可以看到文件几乎相同。tabula 完美地检测到了 2 月的一个,但另一个不是: 不工作照片 工作照
  2. 我无权访问这些 pdf 文件的生成方式,但我猜它们总是遵循相同的程序。因为这不是在新的 pdf 或旧的 pdf 中发生的事情,只是在两者之间随机发生。

我可以做些什么来改变 tabula 如何检测表格以使其更准确?

标签: pythonpdftotexttabula

解决方案


好吧,我给自己一个答案,我是如何解决这个问题的,虽然不是一个干净的解决方案,我仍然不明白为什么 tabula 在某些 pdf 中随机失败。

给定 area 参数,我改变了批处理的方法来一个一个地处理它们。我使用 foxit reader 来大致获取表格所在的像素,在我的情况下幸运的是总是相同的: 在此处输入图像描述

然后我写了这样的代码:

from tabula import convert_into
import os
import re
import sys 

directory=sys.argv[0]
output=sys.argv[1]
files = [f for f in os.listdir(directory) if re.match(r'.+\.pdf', f)]
for file in files:
    print(os.path.join(directory, file))
    convert_into(os.path.join(directory, file), output_path=os.path.join(output, file.replace(".pdf",".csv" )), pages='all', area=[[409,39,750,590]])

我仍然需要做更多的测试,但对于我的场景来说似乎很稳定,虽然很丑,但我愿意看到另一个不需要这个测量系统的解决方案。


推荐阅读