使用 python 将 pdf tables 合并为一个 excel table

concat pdf tables into one excel table using python

我使用 tabula 是为了 concat 以下 pdf file

中的所有 tables

成为 excel 格式内的 table。

这是我的代码:

from tabula import read_pdf
import pandas as pd

allin = []
for page in range(1, 115):
    table = read_pdf("goal.pdf", pages=page,
                     pandas_options={'header': None})[0]
    allin.append(table)


new = pd.concat(allin)

new.to_excel("out.xlsx", index=False)

我也尝试了以下方法:

from tabula import read_pdf
import pandas as pd

table = read_pdf("goal.pdf", pages='all', pandas_options={'header': None})

new = pd.concat(table, ignore_index=True)

new.to_excel("out.xlsx", index=False)

当前输出:check

但是从第 # 91 页开始,我开始看到 excel 文件中的数据格式不正确。

我已经单独调试了页面,我无法弄清楚为什么它的格式错误,尤其是在相同格式的情况下。

from tabula import read_pdf
import pandas as pd

table = read_pdf("goal.pdf", pages='91', pandas_options={'header': None})[0]


print(table)

示例:

from tabula import read_pdf
import pandas as pd

table = read_pdf("goal.pdf", pages='90-91', pandas_options={'header': None})

new = pd.concat(table, ignore_index=True)

new.to_excel("out.xlsx", index=False)

这里是第 90 和 91 两页的代码运行。

从第 48 行开始,您将看到不同之处 here

您会注意到将姓名和地址放在一个单元格中的问题。城市和州也被纳入一个电话

我深入研究了源代码,它有选项 columns,您可以手动定义列边界。当您设置 columns 时,您必须使用 guess=False.

tabula-py 使用程序 tabula-java and in its documentation I found that it needs values in percents or points (not pixels). So I used program inkscape 以点为单位测量边界。

from tabula import read_pdf
import pandas as pd

# display all columns in dataframe
pd.set_option('display.width', None)

columns = [210, 350, 420, 450]  # boundaries in points
#columns = ['210,350,420,450']   # boundaries in points

pages =  '90-92'
#pages = [90,91,92]
#pages = list(range(90,93))
#pages = 'all'  # read all pages 

tables = read_pdf("goal.pdf",
                  pages=pages,
                  pandas_options={'header': None},
                  columns=columns,
                  guess=False)

df = pd.concat(tables).reset_index(drop=True)
#df.rename(columns=df.iloc[0], inplace=True)  # convert first row to headers
#df.drop(df.index[0], inplace=True)           # remove first row with headers 

# display

#for x in range(0, len(df), 20):
#    print(df.iloc[x:x+20])
#    print('----------')

print(df.iloc[45:50])

#df.to_csv('output-pdf.csv')

#print(df[ df['State'].str.contains(' ') ])
#print(df[ df.iloc[:,3].str.contains(' ') ])

结果:

                                      0                         1       2   3               4
45                        JARRARD, GARY      930 FORT WORTH DRIVE  DENTON  TX  (940) 565-6548
46                        JARRARD, GARY        2219 COLORADO BLVD  DENTON  TX  (940) 380-1661
47  MASON HARRISON, RATLIFF ENTERPRISES  1815 W. UNIVERSITY DRIVE  DENTON  TX  (940) 387-5431
48  MASON HARRISON, RATLIFF ENTERPRISES          109 N. LOOP #288  DENTON  TX  (940) 484-2904
49  MASON HARRISON, RATLIFF ENTERPRISES      930 FORT WORTH DRIVE  DENTON  TX  (940) 565-6548

编辑:

可能还需要选项 area(也以点为单位)以跳过 headers。或者您将不得不删除第一页的第一行。

我没有检查所有行,但可能需要对列边界进行一些更改。


编辑:

几行造成问题 - 可能是因为 City 中的文本太长。

col3 = df.iloc[:,3]

print(df[ col3.str.contains(' ') ])

结果:

                              0                       1                 2         3                 4
1941  UMSTATTD RESTAURANTS, LLC  120 WEST US HIGHWAY 54  EL DORADO SPRING      MS O    (417) 876-5755
2079               SIMONS, GARY         1412 BURLINGTON  NORTH KANSAS CIT      MY O    (816) 421-5941
2763       GRISHAM, ROBERT (RB)   403 WEST COURT STREET    WASHINGTON COU  ORTH HOU  S(E740) 335-7830
2764            STAUFFER, JACOB   403 WEST COURT STREET    WASHINGTON COU  ORTH HOU  S(E740) 335-7830