Spaces:
Build error
Build error
| import os | |
| import streamlit as st | |
| from pdf2image import convert_from_path | |
| from PIL import Image | |
| import pytesseract | |
| from docx import Document | |
| # Ensure Poppler's path is correct | |
| # Set the full path to Poppler's 'bin' directory (update this path according to your system) | |
| poppler_path = r'C:\poppler\bin' # Update this with your actual Poppler path | |
| def pdf_to_text(pdf_path): | |
| try: | |
| # Convert PDF to images | |
| images = convert_from_path(pdf_path, poppler_path=poppler_path) | |
| text = "" | |
| # Extract text from each image using pytesseract | |
| for image in images: | |
| text += pytesseract.image_to_string(image) | |
| return text | |
| except Exception as e: | |
| st.error(f"Error during PDF to image conversion: {e}") | |
| return None | |
| def save_text_to_word(text, filename="output.docx"): | |
| # Create a Word document and write the text to it | |
| doc = Document() | |
| doc.add_paragraph(text) | |
| doc.save(filename) | |
| def main(): | |
| st.title("PDF to Text Converter") | |
| # Upload PDF file | |
| uploaded_file = st.file_uploader("Upload a PDF", type="pdf") | |
| if uploaded_file is not None: | |
| # Save uploaded file temporarily | |
| with open("uploaded_file.pdf", "wb") as f: | |
| f.write(uploaded_file.getbuffer()) | |
| st.text("Converting PDF to text...") | |
| # Convert PDF to text | |
| text = pdf_to_text("uploaded_file.pdf") | |
| if text: | |
| st.text_area("Extracted Text", text, height=300) | |
| # Create downloadable Word file | |
| word_file = "output.docx" | |
| save_text_to_word(text, word_file) | |
| st.download_button("Download Word File", word_file) | |
| if __name__ == "__main__": | |
| main() | |