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.")