Skip to main content
 首页 » 编程设计

python之提取跨越多个页面的表格

2024年12月31日798°冷暖

我正在尝试从 pdf 中提取表格。 Tabula 帮助我从 pdf 中提取表格。

目前我面临的问题是,如果任何表格跨越多个页面,Tabula 会将每个新页面表格内容视为新表格。

有什么方法或逻辑可以克服这个问题吗?

代码:

from tabula import read_pdf 
df = read_pdf("SampleTableFormat2pages.pdf", multiple_tables=True, pages="all") 
print len(df) 
print df 

输出

2 
[        0       1       2       3       4 
0  Label1  Label2  Label3  Label4  Label5 
1   Row11   Row12   Row13   Row14   Row15 
2   Row21   Row22   Row23   Row24   Row25 
3   Row31   Row32   Row33   Row34   Row35,        0      1      2      3      4 
0  Row41  Row42  Row43  Row44  Row45 
1  Row51  Row52  Row53  Row54  Row55] 

解释 Tabula 以理解表格边界和下一页跨越的任何逻辑?

或者任何其他图书馆可以帮助解决这个问题?

请您参考如下方法:

我会建议一次转到每个页面并连接最终表。 您可以将此函数用于 pdf 中的页数

import re 
def count_pdf_pages(file_path): 
   rxcountpages = re.compile(r"/Type\s*/Page([^s]|$)", re.MULTILINE|re.DOTALL) 
   with open(file_path, "rb") as temp_file: 
   return len(rxcountpages.findall(temp_file.read())) 

现在循环遍历带有表格的每个页面

df=pd.DataFrame([]) 
df_combine=pd.DataFrame([]) 
for pageiter in range(pages): 
            df = tabula.read_pdf("SampleTableFormat2pages.pdf",pages=pageiter+1, guess=False) 
            #If you want to change the table by editing the columns you can do that here. 
            df_combine=pd.concat([df,df_combine],) #again you can choose between merge or concat as per your need