Spaces:
Build error
Build error
Upload app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,62 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st # data app development
|
| 2 |
+
import subprocess # process in the os
|
| 3 |
+
from subprocess import STDOUT, check_call #os process manipuation
|
| 4 |
+
import os #os process manipuation
|
| 5 |
+
import base64 # byte object into a pdf file
|
| 6 |
+
import camelot as cam # extracting tables from PDFs
|
| 7 |
+
|
| 8 |
+
# to run this only once and it's cached
|
| 9 |
+
@st.cache
|
| 10 |
+
def gh():
|
| 11 |
+
"""install ghostscript on the linux machine"""
|
| 12 |
+
proc = subprocess.Popen('apt-get install -y ghostscript', shell=True, stdin=None, stdout=open(os.devnull,"wb"), stderr=STDOUT, executable="/bin/bash")
|
| 13 |
+
proc.wait()
|
| 14 |
+
|
| 15 |
+
gh()
|
| 16 |
+
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
st.title("PDF Table Extractor")
|
| 20 |
+
st.subheader("with `Camelot` Python library")
|
| 21 |
+
|
| 22 |
+
st.image("https://raw.githubusercontent.com/camelot-dev/camelot/master/docs/_static/camelot.png", width=200)
|
| 23 |
+
|
| 24 |
+
|
| 25 |
+
# file uploader on streamlit
|
| 26 |
+
|
| 27 |
+
input_pdf = st.file_uploader(label = "upload your pdf here", type = 'pdf')
|
| 28 |
+
|
| 29 |
+
st.markdown("### Page Number")
|
| 30 |
+
|
| 31 |
+
page_number = st.text_input("Enter the page # from where you want to extract the PDF eg: 3", value = 1)
|
| 32 |
+
|
| 33 |
+
# run this only when a PDF is uploaded
|
| 34 |
+
|
| 35 |
+
if input_pdf is not None:
|
| 36 |
+
# byte object into a PDF file
|
| 37 |
+
with open("input.pdf", "wb") as f:
|
| 38 |
+
base64_pdf = base64.b64encode(input_pdf.read()).decode('utf-8')
|
| 39 |
+
f.write(base64.b64decode(base64_pdf))
|
| 40 |
+
f.close()
|
| 41 |
+
|
| 42 |
+
# read the pdf and parse it using stream
|
| 43 |
+
table = cam.read_pdf("input.pdf", pages = page_number, flavor = 'stream')
|
| 44 |
+
|
| 45 |
+
st.markdown("### Number of Tables")
|
| 46 |
+
|
| 47 |
+
# display the output after parsing
|
| 48 |
+
st.write(table)
|
| 49 |
+
|
| 50 |
+
# display the table
|
| 51 |
+
|
| 52 |
+
if len(table) > 0:
|
| 53 |
+
|
| 54 |
+
# extract the index value of the table
|
| 55 |
+
|
| 56 |
+
option = st.selectbox(label = "Select the Table to be displayed", options = range(len(table) + 1))
|
| 57 |
+
|
| 58 |
+
st.markdown('### Output Table')
|
| 59 |
+
|
| 60 |
+
# display the dataframe
|
| 61 |
+
|
| 62 |
+
st.dataframe(table[int(option)-1].df)
|