Karn Kumar
Karn Kumar

Reputation: 8816

Unable to read pdf with tabula

I'm getting into the below error while trying to read a pdf file with tabula(tabula-py).

Is there a way to read pdf in python like pandas or some other libs?

please suggest.

>>> 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

One way around i see is pdftotext conversion..

$ pdftotext OpTransactionHistory28-08-2018.pdf

Just looked upon the provide link by @ace and found something relevant:

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

Upvotes: 1

Views: 5447

Answers (1)

chezou
chezou

Reputation: 495

The error of pandas layer often could be caused by the difference in column numbers between tables, since pandas try to extract one DataFrame from tabula-java output. Using multiple_tables=True can avoid this restriction since tabula-py aware boundary of tables.

I also noted this related error but seems different from what I saw. https://github.com/chezou/tabula-py#i-faced-cparsererror-how-can-i-extract-multiple-tables

It would be much appreciated if you can provide your pandas version.

Upvotes: 1

Related Questions