Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parsing PDFs to JSON format #9

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
tabula-py==2.7.0
41 changes: 41 additions & 0 deletions to_json.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import tabula
import pandas as pd
import json
import os

def pdf_to_json(filepath):
json_str = {}
try:
# Read the PDF file and extract its contents to a JSON file
df = tabula.read_pdf(filepath, pages='1')[0]
col_names = ["CURRENCY","SYMBOL","TT BUY","TT SELL",\
"BILL BUY","BILL SELL","TC BUY","TC SELL",
"CN BUY","CN SELL","PC BUY"]
df.columns = col_names
df.iloc[:,2:11] = df.iloc[:,2:11].apply(pd.to_numeric, errors="coerce")
# drop first 3 rows
df.drop(0, inplace=True)
df.drop(1, inplace=True)
df.drop(2, inplace=True)
json_str = df.to_json(orient='records')
except:
print(filepath)

return json_str

def reformat(original_records):
new_records = {}
for record in original_records:
symbol = record['SYMBOL']
values = {k: v for k, v in record.items() if k != 'SYMBOL'}
new_records[symbol] = values
return new_records

if __name__ == "__main__":
filepath = os.path.join('2022', '01', '2022-01-01-14:15.pdf') # 2022/01/2022-01-01-14:15.pdf
data = pdf_to_json(filepath)
# #TODO transform using dataframe
reformatted_data = reformat(json.loads(data))
json_filepath = filepath.replace(".pdf", ".json")
with open(json_filepath, 'w') as f:
json.dump(reformatted_data, f, indent=4)