【问题标题】:Unable to read pdf with tabula无法使用制表符阅读 pdf
【发布时间】:2018-08-29 05:33:33
【问题描述】:

我在尝试使用 tabula(tabula-py) 读取 pdf 文件时遇到以下错误。

有没有办法像 pandas 或其他一些库一样在 python 中读取 pdf?

请提出建议。

>>> from tabula import read_pdf
>>> df = read_pdf('OpTransactionHistory28-08-2018.pdf')
Aug 29, 2018 10:40:27 AM org.apache.pdfbox.pdmodel.font.FileSystemFontProvider loadDiskCache
WARNING: New fonts found, font cache will be re-built
Aug 29, 2018 10:40:27 AM org.apache.pdfbox.pdmodel.font.FileSystemFontProvider <init>
WARNING: Building on-disk font cache, this may take a while
Aug 29, 2018 10:40:32 AM org.apache.pdfbox.pdmodel.font.FileSystemFontProvider <init>
WARNING: Finished building on-disk font cache, found 328 fonts
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/karn/.local/lib/python3.6/site-packages/tabula/wrapper.py", line 119, in read_pdf
    return pd.read_csv(io.BytesIO(output), **pandas_options)
  File "/home/karn/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 678, in parser_f
    return _read(filepath_or_buffer, kwds)
  File "/home/karn/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 446, in _read
    data = parser.read(nrows)
  File "/home/karn/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1036, in read
    ret = self._engine.read(nrows)
  File "/home/karn/.local/lib/python3.6/site-packages/pandas/io/parsers.py", line 1848, in read
    data = self._reader.read(nrows)
  File "pandas/_libs/parsers.pyx", line 876, in pandas._libs.parsers.TextReader.read
  File "pandas/_libs/parsers.pyx", line 891, in pandas._libs.parsers.TextReader._read_low_memory
  File "pandas/_libs/parsers.pyx", line 945, in pandas._libs.parsers.TextReader._read_rows
  File "pandas/_libs/parsers.pyx", line 932, in pandas._libs.parsers.TextReader._tokenize_rows
  File "pandas/_libs/parsers.pyx", line 2112, in pandas._libs.parsers.raise_parser_error
pandas.errors.ParserError: Error tokenizing data. C error: Expected 8 fields in line 4, saw 9

我看到的一种方法是pdftotext 转换..

$ pdftotext OpTransactionHistory28-08-2018.pdf

刚刚查看了@ace 提供的链接,发现了一些相关的内容:

>>> from tabula import read_pdf
>>> df = read_pdf('OpTransactionHistory28-08-2018.pdf', pages='all', encoding='ISO-8859-1', multiple_tables=True)

【问题讨论】:

  • 可能重复:Python tabula-py error (pandas error?) 但那里没有答案。
  • @ace 我看过了,但正如你所说,那里的问题没有答案。
  • 可能相关的 GitHub 问题:12
  • @ace ,我看到的提示很少,可能有用,我正在查看
  • @ace,虽然使用下面的选项它有点工作......&gt;&gt;&gt; df = read_pdf('OpTransactionHistory28-08-2018.pdf', pages='all', encoding='ISO-8859-1', multiple_tables=True)

标签: python-3.x tabula


【解决方案1】:

pandas 层的错误通常可能是由于表之间的列数不同造成的,因为 pandas 尝试从 tabula-java 输出中提取一个 DataFrame。使用 multiple_tables=True 可以避免这种限制,因为 tabula-py 可以识别表格的边界。

我也注意到了这个相关的错误,但似乎与我看到的不同。 https://github.com/chezou/tabula-py#i-faced-cparsererror-how-can-i-extract-multiple-tables

如果您能提供您的熊猫版本,我们将不胜感激。

【讨论】:

  • @chezou .. 感谢您的意见,我的熊猫版本是&gt;&gt;&gt; pd.__version__ '0.21.0
  • 已经使用 multiple_tables=Trueencoding='ISO-8859-1' 并且有效。
  • @pygo 感谢您的信息!我会在调查后添加FAQ。
  • @chezou .. thnx
  • 你有没有得到一些东西或添加了一些常见问题解答。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-07-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多