Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,71 +1,53 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pytesseract
|
| 3 |
-
from pdf2image import convert_from_path
|
| 4 |
from PIL import Image
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
import io
|
| 8 |
-
import tempfile
|
| 9 |
|
| 10 |
-
#
|
| 11 |
-
|
| 12 |
-
# Save the uploaded file to a temporary file
|
| 13 |
-
with tempfile.NamedTemporaryFile(delete=False) as tmp_file:
|
| 14 |
-
tmp_file.write(uploaded_file.read()) # Write the file content to the temporary file
|
| 15 |
-
tmp_file_path = tmp_file.name # Get the temporary file path
|
| 16 |
-
|
| 17 |
-
# Convert PDF to images using pdf2image
|
| 18 |
-
images = convert_from_path(tmp_file_path)
|
| 19 |
-
extracted_text = []
|
| 20 |
-
|
| 21 |
-
for image in images:
|
| 22 |
-
# Use pytesseract to do OCR on the image
|
| 23 |
-
text = pytesseract.image_to_string(image)
|
| 24 |
-
extracted_text.append(text)
|
| 25 |
-
|
| 26 |
-
return "\n".join(extracted_text)
|
| 27 |
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
doc = Document()
|
| 31 |
-
doc.add_paragraph(text)
|
| 32 |
-
doc.save(output_filename)
|
| 33 |
|
| 34 |
-
#
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
-
# Main function
|
| 44 |
def main():
|
| 45 |
-
|
| 46 |
|
| 47 |
-
|
| 48 |
-
|
|
|
|
| 49 |
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
extracted_text = extract_text_from_image_pdf(uploaded_file)
|
| 53 |
|
| 54 |
-
|
| 55 |
-
|
|
|
|
| 56 |
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
word_filename = "extracted_text.docx"
|
| 61 |
-
save_to_word(extracted_text, word_filename)
|
| 62 |
-
st.success(f"Saved to {word_filename}")
|
| 63 |
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
|
| 70 |
if __name__ == "__main__":
|
| 71 |
-
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pytesseract
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import docx
|
| 5 |
+
import pdf2image
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# Set Tesseract path if not set already
|
| 8 |
+
pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
|
| 10 |
+
def extract_text_from_image_pdf(pdf_file):
|
| 11 |
+
"""Extracts text from a PDF by converting it to images and performing OCR."""
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
+
# Read the PDF file
|
| 14 |
+
with open(pdf_file, 'rb') as f:
|
| 15 |
+
pdf_bytes = f.read()
|
| 16 |
+
|
| 17 |
+
# Extract images from the PDF
|
| 18 |
+
images = pdf2image.convert_from_bytes(pdf_bytes)
|
| 19 |
+
|
| 20 |
+
# Perform OCR on each image and combine the text
|
| 21 |
+
extracted_text = ''
|
| 22 |
+
for image in images:
|
| 23 |
+
text = pytesseract.image_to_string(image)
|
| 24 |
+
extracted_text += text + '\n' # Add newline for better readability
|
| 25 |
+
|
| 26 |
+
return extracted_text
|
| 27 |
|
|
|
|
| 28 |
def main():
|
| 29 |
+
"""Streamlit app for converting PDF images to text."""
|
| 30 |
|
| 31 |
+
# Title and description
|
| 32 |
+
st.title("PDF to Text Converter")
|
| 33 |
+
st.subheader("Convert your PDF images to editable text documents.")
|
| 34 |
|
| 35 |
+
# Upload PDF file
|
| 36 |
+
uploaded_file = st.file_uploader("Choose a PDF file to convert:", type="pdf")
|
|
|
|
| 37 |
|
| 38 |
+
if uploaded_file is not None:
|
| 39 |
+
# Extract text from the PDF
|
| 40 |
+
extracted_text = extract_text_from_image_pdf(uploaded_file.name)
|
| 41 |
|
| 42 |
+
# Display extracted text
|
| 43 |
+
st.success("Text extracted from PDF:")
|
| 44 |
+
st.write(extracted_text)
|
|
|
|
|
|
|
|
|
|
| 45 |
|
| 46 |
+
# Download option (optional)
|
| 47 |
+
if st.button("Download text as .txt file"):
|
| 48 |
+
with open("extracted_text.txt", "w") as f:
|
| 49 |
+
f.write(extracted_text)
|
| 50 |
+
st.success("Text downloaded!")
|
| 51 |
|
| 52 |
if __name__ == "__main__":
|
| 53 |
+
main()
|