Spaces:
Build error
Build error
File size: 2,104 Bytes
522330d 6fc11e7 522330d 6fc11e7 522330d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 | import streamlit as st
from PyPDF2 import PdfReader
from docx import Document
import pandas as pd
# Define functions for extracting text from different document types
def extract_text_from_pdf(pdf_file):
reader = PdfReader(pdf_file)
text = ''
for page in reader.pages:
text += page.extract_text()
return text
def extract_text_from_docx(docx_file):
doc = Document(docx_file)
text = ''
for para in doc.paragraphs:
text += para.text
return text
def extract_text_from_excel(excel_file):
df = pd.read_excel(excel_file)
return df.to_string()
# Streamlit UI elements
st.title("Document Text Extractor")
# Upload multiple files of specific types
uploaded_files = st.file_uploader("Upload your files", type=['pdf', 'docx', 'xlsx'], accept_multiple_files=True)
# If the 'Extract Text' button is clicked
if st.button("Extract Text"):
complete_text = ""
if uploaded_files is not None:
# Loop through all the uploaded files
for uploaded_file in uploaded_files:
# Identify the file type and extract text accordingly
if uploaded_file.name.endswith('.pdf'):
complete_text += extract_text_from_pdf(uploaded_file)
elif uploaded_file.name.endswith('.docx'):
complete_text += extract_text_from_docx(uploaded_file)
elif uploaded_file.name.endswith('.xlsx'):
complete_text += extract_text_from_excel(uploaded_file)
# Display the extracted text
st.text_area("Extracted Text", complete_text, height=300)
# Save the extracted text to a local file
if st.button("Save to Local"):
with open("extracted_text.txt", "w", encoding="utf-8") as file:
file.write(complete_text)
st.success("Text saved locally as `extracted_text.txt`!")
# Download the extracted text as a file
st.download_button("Download as Text File", complete_text, file_name="extracted_text.txt")
else:
st.write("Please upload at least one file to extract text.")
|