2

I am trying to extract only tables from pdf using tabula package and writing the output into csv, Unfortunately, the below code gives me an error as "NameError: name 'tabula' is not defined"

How to fix this issue

Code:

!pip install tabula-py
from tabula import read_pdf
from tabula.io import read_pdf
file = r"url"
df = read_pdf(file, pages='all')
tabula.to_csv('output.csv', encoding='utf-8')

Error:

"NameError: name 'tabula' is not defined"
  • This is not causing the `NameError` but it is an issue: `from tabula.io import read_pdf` is clobbering `from tabula import read_pdf`. Each of these functions is assigned the same name in the current scope. You should figure out which one you want and only import it. As a general rule, don't import different things under the same name. – Michael Ruth Mar 15 '21 at 09:13

2 Answers2

0

Heres an explanation. Every time you use from module import function it will take the function, not the entire library and the functions, so if you want to use that tabula.to_csv() function, you will need to import the whole library, using import tabula.

Other Method:
You can use to_csv() and import it with from tabula import to_csv

Dharman
  • 30,962
  • 25
  • 85
  • 135
Quebeh
  • 134
  • 10
  • I am getting the error while "from tabula import to_csv" : cannot import name 'to_csv' from 'tabula' (C:\Users\userid\Anaconda3\lib\site-packages\tabula\__init__.py) – Learn with Kumaran Mar 15 '21 at 09:25
  • Oh, it means there is no function named to_csv in the tabula module, you can take a look at the tabula article or you can use the first method instead – Quebeh Mar 15 '21 at 09:27
-1
from tabula import read_pdf
from tabula.io import read_pdf
file = r"url"
df = read_pdf(file, pages='all')
tabula.to_csv('output.csv', encoding='utf-8') # from tabula import to_csv

You using the method but not imported so, it's giving this error

KLAS R
  • 52
  • 7
  • In official documentation says "tabula.convert_into("test.pdf", "output.csv", output_format="csv", pages='all')", but the same is not working for me, and it gives me the same error "NameError: name 'tabula' is not defined" – Learn with Kumaran Mar 15 '21 at 09:55
  • Also, I am able to get the output by calling "df", but help me how to write the same output into CSV – Learn with Kumaran Mar 15 '21 at 09:58