Spaces:
Build error
Build error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
from PyPDF2 import PdfReader
|
| 3 |
+
from docx import Document
|
| 4 |
+
import pandas as pd
|
| 5 |
+
|
| 6 |
+
# Define functions for extracting text from different document types
|
| 7 |
+
def extract_text_from_pdf(pdf_file):
|
| 8 |
+
reader = PdfReader(pdf_file)
|
| 9 |
+
text = ''
|
| 10 |
+
for page in reader.pages:
|
| 11 |
+
text += page.extract_text()
|
| 12 |
+
return text
|
| 13 |
+
|
| 14 |
+
def extract_text_from_docx(docx_file):
|
| 15 |
+
doc = Document(docx_file)
|
| 16 |
+
text = ''
|
| 17 |
+
for para in doc.paragraphs:
|
| 18 |
+
text += para.text
|
| 19 |
+
return text
|
| 20 |
+
|
| 21 |
+
def extract_text_from_excel(excel_file):
|
| 22 |
+
df = pd.read_excel(excel_file)
|
| 23 |
+
return df.to_string()
|
| 24 |
+
|
| 25 |
+
# Streamlit UI elements
|
| 26 |
+
st.title("Document Text Extractor")
|
| 27 |
+
|
| 28 |
+
# Upload multiple files of specific types
|
| 29 |
+
uploaded_files = st.file_uploader("Upload your files", type=['pdf', 'docx', 'xlsx'], accept_multiple_files=True)
|
| 30 |
+
|
| 31 |
+
# If the 'Extract Text' button is clicked
|
| 32 |
+
if st.button("Extract Text"):
|
| 33 |
+
complete_text = ""
|
| 34 |
+
if uploaded_files is not None:
|
| 35 |
+
# Loop through all the uploaded files
|
| 36 |
+
for uploaded_file in uploaded_files:
|
| 37 |
+
# Identify the file type and extract text accordingly
|
| 38 |
+
if uploaded_file.name.endswith('.pdf'):
|
| 39 |
+
complete_text += extract_text_from_pdf(uploaded_file)
|
| 40 |
+
elif uploaded_file.name.endswith('.docx'):
|
| 41 |
+
complete_text += extract_text_from_docx(uploaded_file)
|
| 42 |
+
elif uploaded_file.name.endswith('.xlsx'):
|
| 43 |
+
complete_text += extract_text_from_excel(uploaded_file)
|
| 44 |
+
|
| 45 |
+
# Display the extracted text
|
| 46 |
+
st.text_area("Extracted Text", complete_text, height=300)
|
| 47 |
+
else:
|
| 48 |
+
st.write("Please upload at least one file to extract text.")
|
| 49 |
+
|
| 50 |
+
|