I have PDF file whose 1st page data format is different however rest of the pages has the same tabular format. I want to convert this PDF file which has multiple pages into a CSV file using Python Tabula.
The current code is able to convert PDF to CSV if the PDF has only 2 pages and if it has more that two pages it gives error out of range.
I want to count total number of PDF pages of a PDF File and depending upon the same I want python script to convert the PDF to CSV for different data frames.
I am using Linux box to run this python script.
The code is as given below:
#!/usr/bin/env python3
import tabula
import pandas as pd
import csv
pdf_file='/root/scripts/pdf2xls/Test/21KJAZP011.pdf'
column_names=['Product','Batch No','Machin No','Time','Date','Drum/Bag No','Tare Wt.kg','Gross Wt.kg',
'Net Wt.kg','Blender','Remarks','Operator']
df_results=[] # store results in a list
# Page 1 processing
try:
df1 = tabula.read_pdf('/root/scripts/pdf2xls/Test/21KJAZP011.pdf', pages=1,area=(95,20, 800, 840),columns=[93,180,220,252,310,315,333,367,
410,450,480,520]
,pandas_options={'header': None}) #(top,left,bottom,right)
df1[0]=df1[0].drop(columns=5)
df1[0].columns=column_names
df_results.append(df1[0])
df1[0].head(2)
except Exception as e:
print(f"Exception page not found {e}")
# Page 2 processing
try:
df2 = tabula.read_pdf('/root/scripts/pdf2xls/Test/21KJAZP011.pdf', pages=2,area=(10,20, 800, 840),columns=[93,180,220,252,310,315,330,370,
410,450,480,520]
,pandas_options={'header': None}) #(top,left,bottom,right)
row_with_Sta = df2[0][df2[0][0] == 'Sta'].index.tolist()[0]
df2[0] = df2[0].iloc[:row_with_Sta]
df2[0]=df2[0].drop(columns=5)
df2[0].columns=column_names
df_results.append(df2[0])
df2[0].head(2)
except Exception as e:
print(f"Exception page not found {e}")
#res:wult = pd.concat([df1[0],df2[0],df3[0]]) # concate both the pages and then write to CSV
result = pd.concat(df_results) # concate list of pages and then write to CSV
result.to_csv("result.csv")
with open('/root/scripts/pdf2xls/Test/result.csv', 'r') as f_input, open('/root/scripts/pdf2xls/Test/FinalOutput_21KJAZP011.csv', 'w') as f_output:
csv_input = csv.reader(f_input)
csv_output = csv.writer(f_output)
csv_output.writerow(next(csv_input)) # write header
for cols in csv_input:
for i in range(7, 9):
cols[i] = '{:.2f}'.format(float(cols[i]))
csv_output.writerow(cols)
Please suggest how can achieve the same. I am very new to Python and hence unable to put together things.
CodePudding user response:
Try pdfpumber https://github.com/jsvine/pdfplumber, worked for me like a charm
pdffile = 'your file'
with pdfplumber.open(pdffile) as pdf:
for i in range(len(pdf.pages)):
first_page = pdf.pages[i]
rawdata = first_page.extract_table()
CodePudding user response:
Extract Multiple Tables from PDF using multiple_tables options using tabula
multiple_tables=True
from tabula import convert_into
table_file = r"PDF_path"
output_csv = r"out_csv"
df = convert_into(table_file, output_csv, output_format='csv', lattice=False, stream=True, multiple_tables=True, pages="all")