tjxj commited on
Commit
5b58521
·
1 Parent(s): ca92f13

Add application file

Browse files
.history/app_20220619230549.py ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #-*- coding : utf-8-*-
2
+
3
+ import pandas as pd
4
+ import streamlit as st
5
+ import base64
6
+ import subprocess # process in the os
7
+ from subprocess import STDOUT #os process manipuation
8
+ import os
9
+ import camelot as cam # extracting tables from PDFs
10
+
11
+ os.system("apt-get install -y ghostscript")
12
+
13
+ st.title("PDF Table Extractor")
14
+
15
+ input_pdf = st.file_uploader(label = "", type = 'pdf')
16
+
17
+ page_number = st.text_input("请填写表格所在PDF页码,eg: 3", value = 1)
18
+
19
+ if input_pdf is not None:
20
+ # byte object into a PDF file
21
+ with open("input.pdf", "wb") as f:
22
+ base64_pdf = base64.b64encode(input_pdf.read()).decode('utf-8')
23
+ f.write(base64.b64decode(base64_pdf))
24
+ f.close()
25
+
26
+ # read the pdf and parse it using stream
27
+ tables = cam.read_pdf("input.pdf", pages=page_number)
28
+ result = pd.ExcelWriter('result.xlsx', engine='xlsxwriter')
29
+ tables[0].to_excel(result,index=False)
30
+ # for i in range(0,len(tables)):
31
+ # table = tables[i].df
32
+ # sheetname = str(i)
33
+ # table.to_excel(result, sheetname,index=False)
34
+
35
+ with open('result.xlsx','rb') as f:
36
+ st.download_button('提取完成,点击下载!', f,file_name='result.xlsx',mime="application/vnd.ms-excel")
.history/app_20220620143432.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #-*- coding : utf-8-*-
2
+
3
+ import pandas as pd
4
+ import streamlit as st
5
+ import base64
6
+ import subprocess # process in the os
7
+ from subprocess import STDOUT #os process manipuation
8
+ import os
9
+ import camelot as cam # extracting tables from PDFs
10
+
11
+ @st.cache
12
+ def gh():
13
+ """install ghostscript on the linux machine"""
14
+ proc = subprocess.Popen('apt-get install -y ghostscript', shell=True, stdin=None, stdout=open(os.devnull,"wb"), stderr=STDOUT, executable="/bin/bash")
15
+ proc.wait()
16
+
17
+ gh()
18
+ st.title("PDF Table Extractor")
19
+
20
+ input_pdf = st.file_uploader(label = "", type = 'pdf')
21
+
22
+ page_number = st.text_input("请填写表格所在PDF页码,eg: 3", value = 1)
23
+
24
+ if input_pdf is not None:
25
+ # byte object into a PDF file
26
+ with open("input.pdf", "wb") as f:
27
+ base64_pdf = base64.b64encode(input_pdf.read()).decode('utf-8')
28
+ f.write(base64.b64decode(base64_pdf))
29
+ f.close()
30
+
31
+ # read the pdf and parse it using stream
32
+ tables = cam.read_pdf("input.pdf", pages=page_number)
33
+ result = pd.ExcelWriter('result.xlsx', engine='xlsxwriter')
34
+ tables[0].to_excel(result,index=False)
35
+ # for i in range(0,len(tables)):
36
+ # table = tables[i].df
37
+ # sheetname = str(i)
38
+ # table.to_excel(result, sheetname,index=False)
39
+
40
+ with open('result.xlsx','rb') as f:
41
+ st.download_button('提取完成,点击下载!', f,file_name='result.xlsx',mime="application/vnd.ms-excel")
.history/app_20220620143433.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #-*- coding : utf-8-*-
2
+
3
+ import pandas as pd
4
+ import streamlit as st
5
+ import base64
6
+ import subprocess # process in the os
7
+ from subprocess import STDOUT #os process manipuation
8
+ import os
9
+ import camelot as cam # extracting tables from PDFs
10
+
11
+ @st.cache
12
+ def gh():
13
+ """install ghostscript on the linux machine"""
14
+ proc = subprocess.Popen('apt-get install -y ghostscript', shell=True, stdin=None, stdout=open(os.devnull,"wb"), stderr=STDOUT, executable="/bin/bash")
15
+ proc.wait()
16
+
17
+ gh()
18
+ st.title("PDF Table Extractor")
19
+
20
+ input_pdf = st.file_uploader(label = "", type = 'pdf')
21
+
22
+ page_number = st.text_input("请填写表格所在PDF页码,eg: 3", value = 1)
23
+
24
+ if input_pdf is not None:
25
+ # byte object into a PDF file
26
+ with open("input.pdf", "wb") as f:
27
+ base64_pdf = base64.b64encode(input_pdf.read()).decode('utf-8')
28
+ f.write(base64.b64decode(base64_pdf))
29
+ f.close()
30
+
31
+ # read the pdf and parse it using stream
32
+ tables = cam.read_pdf("input.pdf", pages=page_number)
33
+ result = pd.ExcelWriter('result.xlsx', engine='xlsxwriter')
34
+ tables[0].to_excel(result,index=False)
35
+ # for i in range(0,len(tables)):
36
+ # table = tables[i].df
37
+ # sheetname = str(i)
38
+ # table.to_excel(result, sheetname,index=False)
39
+
40
+ with open('result.xlsx','rb') as f:
41
+ st.download_button('提取完成,点击下载!', f,file_name='result.xlsx',mime="application/vnd.ms-excel")
README 2.md DELETED
@@ -1,2 +0,0 @@
1
- # extract-table-from-pdf
2
- extract table from pdf and save as Excel files
 
 
 
app.py CHANGED
@@ -8,8 +8,13 @@ from subprocess import STDOUT #os process manipuation
8
  import os
9
  import camelot as cam # extracting tables from PDFs
10
 
11
- os.system("apt-get install -y ghostscript")
 
 
 
 
12
 
 
13
  st.title("PDF Table Extractor")
14
 
15
  input_pdf = st.file_uploader(label = "", type = 'pdf')
 
8
  import os
9
  import camelot as cam # extracting tables from PDFs
10
 
11
+ @st.cache
12
+ def gh():
13
+ """install ghostscript on the linux machine"""
14
+ proc = subprocess.Popen('apt-get install -y ghostscript', shell=True, stdin=None, stdout=open(os.devnull,"wb"), stderr=STDOUT, executable="/bin/bash")
15
+ proc.wait()
16
 
17
+ gh()
18
  st.title("PDF Table Extractor")
19
 
20
  input_pdf = st.file_uploader(label = "", type = 'pdf')
bar.py DELETED
@@ -1,24 +0,0 @@
1
- import gradio as gr
2
- import camelot
3
- import pandas as pd
4
- import base64
5
- def table_extracter(file):
6
-
7
- with open("input.pdf", "wb") as f:
8
- base64_pdf = base64.b64encode(file.read()).decode('utf-8')
9
- f.write(base64.b64decode(base64_pdf))
10
- f.close()
11
-
12
- # read the pdf and parse it using stream
13
- tables = camelot.read_pdf(file,pages='1-4')
14
- result = pd.ExcelWriter('result.xlsx')
15
- for i in range(0,len(tables)):
16
- table = tables[i].df
17
- sheetname = str(i)
18
- table.to_excel(result, sheetname,index=False)
19
- return 'result.xlsx'
20
-
21
- demo = gr.Interface(fn=table_extracter, inputs="file",
22
- outputs="file",title = 'PDF into table Application',
23
- description = 'Simple application in python to try the Gradio package components')
24
- demo.launch()