beihai/PDF-Table-Extractor
2
1#-*- coding : utf-8-*-2import base643from subprocess import STDOUT 4import streamlit as st5import pandas as pd 6import camelot as cam # extracting tables from PDFs 7 8st.title("PDF Table Extractor")9input_pdf = st.file_uploader(label = "", type = 'pdf')10background = st.selectbox("表格线条是否透明",(False,True))11page_number = st.text_input("请填写表格所在PDF页码,eg: 3, 1-3, 2-end, all", value = 1)12 13if input_pdf is not None:14 # byte object into a PDF file 15 with open("input.pdf", "wb") as f:16 base64_pdf = base64.b64encode(input_pdf.read()).decode('utf-8')17 f.write(base64.b64decode(base64_pdf))18 f.close()19 tables_all= cam.read_pdf("input.pdf", pages=page_number, process_background=background)20 result_all = pd.ExcelWriter("result.xlsx", engine='xlsxwriter') 21 for i in range(0,len(tables_all)):22 table = tables_all[i].df23 sheetname = str(i)24 table.to_excel(result_all, sheetname,index=False) 25 result_all.save()26 with open(result_all,'rb') as f:27 st.download_button('抽取完成, 点击下载!', f,file_name="result.xlsx",mime="application/vnd.ms-excel")