tjxj commited on
Commit
8f35a06
·
1 Parent(s): 97508c1

Add application file

Browse files
Files changed (2) hide show
  1. .history/app_20220620160542.py +41 -0
  2. app.py +1 -0
.history/app_20220620160542.py ADDED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #-*- coding : utf-8-*-
2
+ import pandas as pd
3
+ import streamlit as st
4
+ import os,base64,subprocess
5
+ from subprocess import STDOUT #os process manipuation
6
+ os.system("apt-get install -y ghostscript")
7
+
8
+ @st.cache
9
+ def gh():
10
+ """install ghostscript on the linux machine"""
11
+ proc = subprocess.Popen('apt-get install -y ghostscript', shell=True, stdin=None, stdout=open(os.devnull,"wb"), stderr=STDOUT, executable="/bin/bash")
12
+ proc.wait()
13
+
14
+ gh()
15
+
16
+ import camelot as cam
17
+
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")
app.py CHANGED
@@ -3,6 +3,7 @@ import pandas as pd
3
  import streamlit as st
4
  import os,base64,subprocess
5
  from subprocess import STDOUT #os process manipuation
 
6
 
7
  @st.cache
8
  def gh():
 
3
  import streamlit as st
4
  import os,base64,subprocess
5
  from subprocess import STDOUT #os process manipuation
6
+ os.system("apt-get install -y ghostscript")
7
 
8
  @st.cache
9
  def gh():