Task:
PDF which is a bank statement,contains columns i.e (Date,Description,Deposits,Withdrawals,Balance) parsing the columns with their respective fields and export that data in CSV format.PDF.
My code:
import pdftotext
import re
import csv
# open PDF file
with open('test.pdf', 'rb') as pdf_file:
pdf = pdftotext.PDF(pdf_file)
# extract tabular text
lines = pdf[2].split('\n')[4:]
# CSV table
table = []
# loop over lines in table
for line in lines:
# replace trailing spaces with comas
row = re.sub(' ', ',', line)
# reducing the number of comas to one
row = [cols.strip() for cols in re.sub(',+', ',', row).split(',')]
# handling missed separators
row = ','.join(row).replace(' ', ',').split(',')
# append row to table
table.append(row)
print(table)
# write CSV output
with open('test.csv', 'w') as csv_file:
writer = csv.writer(csv_file)
writer.writerows(table)
Problem:
I am not getting the desired output i.e half of the description is showing under the date table.I am attaching the csv for further ref here.
Desired Output:
E.g
['04/02','CHRYSLER CAPITAL PAYMENT 0023582513','$469.88-','$51.15']