AzizWazir commited on
Commit
09938bc
·
verified ·
1 Parent(s): d7b6485

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -13
app.py CHANGED
@@ -1,31 +1,38 @@
 
1
  from docx import Document
 
2
 
3
- def extract_text_from_docx(file_path):
4
- """Extracts all text from a .docx file"""
5
  try:
6
- # Open the .docx file
7
- doc = Document(file_path)
8
 
9
  # Extract text from each paragraph in the document
10
  text = ""
11
  for paragraph in doc.paragraphs:
12
  text += paragraph.text + '\n'
13
-
14
  return text
15
 
16
  except Exception as e:
17
- print(f"Error processing the document: {e}")
18
  return None
19
 
20
  def main():
21
- file_path = "your_document.docx" # Replace with your actual file path
22
- text = extract_text_from_docx(file_path)
23
 
24
- if text:
25
- print("Extracted Text:")
26
- print(text)
27
- else:
28
- print("Failed to extract text.")
 
 
 
 
 
 
29
 
30
  if __name__ == "__main__":
31
  main()
 
1
+ import streamlit as st
2
  from docx import Document
3
+ import io
4
 
5
+ def extract_text_from_docx(file):
6
+ """Extracts all text from an uploaded .docx file"""
7
  try:
8
+ # Open the uploaded .docx file
9
+ doc = Document(io.BytesIO(file.read()))
10
 
11
  # Extract text from each paragraph in the document
12
  text = ""
13
  for paragraph in doc.paragraphs:
14
  text += paragraph.text + '\n'
15
+
16
  return text
17
 
18
  except Exception as e:
19
+ st.error(f"Error processing the document: {e}")
20
  return None
21
 
22
  def main():
23
+ st.title("Extract Text from DOCX")
 
24
 
25
+ # File upload
26
+ uploaded_file = st.file_uploader("Choose a DOCX file", type=["docx"])
27
+
28
+ if uploaded_file is not None:
29
+ text = extract_text_from_docx(uploaded_file)
30
+
31
+ if text:
32
+ st.subheader("Extracted Text")
33
+ st.text(text)
34
+ else:
35
+ st.error("Failed to extract text.")
36
 
37
  if __name__ == "__main__":
38
  main()