Nalla commited on
Commit
dfb4bfd
·
1 Parent(s): 6e553ba

Upload App_For_PDF_To_Dataframe.py

Browse files
Files changed (1) hide show
  1. App_For_PDF_To_Dataframe.py +87 -0
App_For_PDF_To_Dataframe.py ADDED
@@ -0,0 +1,87 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # -*- coding: utf-8 -*-
2
+ """
3
+ Created on Sat Feb 19 20:23:31 2022
4
+
5
+ @author: nperuma
6
+ """
7
+
8
+ import streamlit as st # data app development
9
+ import subprocess # process in the os
10
+ from subprocess import STDOUT, check_call #os process manipuation
11
+ import os #os process manipuation
12
+ import base64 # byte object into a pdf file
13
+ import camelot as cam # extracting tables from PDFs
14
+
15
+ # to run this only once and it's cached
16
+ @st.cache
17
+ def gh():
18
+ """install ghostscript on the linux machine"""
19
+ proc = subprocess.Popen('apt-get install -y ghostscript', shell=True, stdin=None, stdout=open(os.devnull,"wb"), stderr=STDOUT, executable="/bin/bash")
20
+ proc.wait()
21
+
22
+ gh()
23
+
24
+
25
+
26
+ st.title("PDF Table Extractor")
27
+ st.subheader("Extract the contents in ease")
28
+
29
+ st.image("https://raw.githubusercontent.com/camelot-dev/camelot/master/docs/_static/camelot.png", width=150)
30
+
31
+
32
+
33
+ # file uploader on streamlit
34
+
35
+ input_pdf = st.file_uploader(label = "upload your pdf here", type = 'pdf')
36
+
37
+ st.markdown("### Page Number")
38
+
39
+ page_number = st.text_input("Enter the page # from where you want to extract the PDF eg: 3", value = 1)
40
+
41
+ # run this only when a PDF is uploaded
42
+
43
+ if input_pdf is not None:
44
+ # byte object into a PDF file
45
+ with open("input.pdf", "wb") as f:
46
+ base64_pdf = base64.b64encode(input_pdf.read()).decode('utf-8')
47
+ f.write(base64.b64decode(base64_pdf))
48
+ f.close()
49
+
50
+ # read the pdf and parse it using stream
51
+ table = cam.read_pdf("input.pdf", pages = page_number, flavor = 'stream')
52
+
53
+ st.markdown("### Number of Tables")
54
+
55
+ # display the output after parsing
56
+ st.write(table)
57
+
58
+ # display the table
59
+
60
+ if len(table) > 0:
61
+
62
+ # extract the index value of the table
63
+
64
+ option = st.selectbox(label = "Select the Table to be displayed", options = range(len(table) + 1))
65
+
66
+ st.markdown('### Output Table')
67
+
68
+ # display the dataframe
69
+
70
+ st.dataframe(table[int(option)-1].df)
71
+
72
+
73
+
74
+ @st.cache
75
+ def convert_df(df):
76
+ # IMPORTANT: Cache the conversion to prevent computation on every rerun
77
+ return df.to_csv().encode('utf-8')
78
+
79
+ csv = convert_df(table[int(option)-1].df)
80
+
81
+ st.download_button(
82
+ label="Download data as CSV",
83
+ data=csv,
84
+ file_name='Data_table.csv',
85
+ mime='text/csv',
86
+ )
87
+