Python/문서 데이터 분석

[Python] [tabula-py] PDF 파일 정보 추출

데이터 세상 2022. 1. 10. 13:19

tabula-py

https://github.com/chezou/tabula-py

 

GitHub - chezou/tabula-py: Simple wrapper of tabula-java: extract table from PDF into pandas DataFrame

Simple wrapper of tabula-java: extract table from PDF into pandas DataFrame - GitHub - chezou/tabula-py: Simple wrapper of tabula-java: extract table from PDF into pandas DataFrame

github.com

tabula-py를 이용할 경우 PDF 파일 내의 테이블 정보를 pandas의 Dataframe으로 추출할 수 있다.

 

tabula-py 설치

pip install tabula-py

 

tabula-py를 이용한 파일 정보 추출

import tabula

dfs = tabula.read_pdf(
	"https://github.com/chezou/tabula-py/raw/master/tests/resources/data.pdf", 
	pages="all", 
	stream=True
)
print(f"Data Type :{type(dfs)}")
print(f"Data Length: {len(dfs)}")
for index, table in enumerate(dfs):
  print(f"\nData Index: {index}")
  print(type(table))
  print(table.head())

결과

Data Type :<class 'list'>
Data Length: 4

Data Index: 0
<class 'pandas.core.frame.DataFrame'>
          Unnamed: 0   mpg  cyl   disp   hp  drat     wt   qsec  vs  am  gear  carb
0          Mazda RX4  21.0    6  160.0  110  3.90  2.620  16.46   0   1     4     4
1      Mazda RX4 Wag  21.0    6  160.0  110  3.90  2.875  17.02   0   1     4     4
2         Datsun 710  22.8    4  108.0   93  3.85  2.320  18.61   1   1     4     1
3     Hornet 4 Drive  21.4    6  258.0  110  3.08  3.215  19.44   1   0     3     1
4  Hornet Sportabout  18.7    8  360.0  175  3.15  3.440  17.02   0   0     3     2

Data Index: 1
<class 'pandas.core.frame.DataFrame'>
   Sepal.Length  Sepal.Width  Petal.Length  Petal.Width Species
0           5.1          3.5           1.4          0.2  setosa
1           4.9          3.0           1.4          0.2  setosa
2           4.7          3.2           1.3          0.2  setosa
3           4.6          3.1           1.5          0.2  setosa
4           5.0          3.6           1.4          0.2  setosa

Data Index: 2
<class 'pandas.core.frame.DataFrame'>
   Unnamed: 0  Sepal.Length  Sepal.Width  Petal.Length  Petal.Width    Species
0         145           6.7          3.3           5.7          2.5  virginica
1         146           6.7          3.0           5.2          2.3  virginica
2         147           6.3          2.5           5.0          1.9  virginica
3         148           6.5          3.0           5.2          2.0  virginica
4         149           6.2          3.4           5.4          2.3  virginica

Data Index: 3
<class 'pandas.core.frame.DataFrame'>
    len supp  dose
0   4.2   VC   0.5
1  11.5   VC   0.5
2   7.3   VC   0.5
3   5.8   VC   0.5
4   6.4   VC   0.5

 

반응형