如何使用python将所有表从pdf文件存储到Excel工作表?

问题描述 投票:0回答:1

1。我能够获取PDF文件的所有表。但是当我要存储所有表时,只有最后一个表被保存在我的Excel工作表中。

2。如何处理这些被覆盖的值。

3.for循环最后一个表格将保存在excel中

import PyPDF2
import tabula
from tabula import read_pdf
import pandas as pd 
from xlwt import Workbook 



pdfFileObj = open('LAB.pdf', 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
print(pdfReader.numPages)          #Total number of pages 
pageObj = pdfReader.getPage(5)



#LAB is my pdf file
x = tabula.read_pdf("LAB.pdf", pages='all', multiple_tables=True)
for i in x:    #x values in list []
    print("printing all the table from the sheet", i)
    df = pd.DataFrame(i)
df.to_excel('tables.xlsx', header=True, index = True)
python excel pandas tabular
1个回答
0
投票

您可以将pandas数据框附加到单个数据框

df = pd.DataFrame()
for i in x:    #x values in list []
    print("printing all the table from the sheet", i)
    df_table = pd.DataFrame(i)
    df = df.append(df_table)

df.to_excel('tables.xlsx', header=True, index = True)

为了将其存储在单独的excel中,您需要在for循环下运行df.to_excel()。>

for i in x:    #x values in list []
    print("printing all the table from the sheet", i)
    df.to_excel('tables{}.xlsx'.format(i), header=True, index = True)
© www.soinside.com 2019 - 2024. All rights reserved.