Lohith01 commited on
Commit
92cb1d3
·
verified ·
1 Parent(s): 65ed1d0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +25 -18
app.py CHANGED
@@ -1,14 +1,15 @@
1
  import PyPDF2
2
  import openpyxl
3
  from bs4 import BeautifulSoup
4
- import streamlit as st
5
  import os
 
6
 
7
  def extract_pdf_text(pdf_file):
8
- reader = PyPDF2.PdfReader(pdf_file)
9
- text = ''
10
- for page in reader.pages:
11
- text += page.extract_text()
 
12
  return text
13
 
14
  def extract_excel_text(excel_file):
@@ -21,31 +22,37 @@ def extract_excel_text(excel_file):
21
  return text
22
 
23
  def extract_html_text(html_file):
24
- soup = BeautifulSoup(html_file, 'html.parser')
25
- text = soup.get_text() # Extract visible text
 
 
 
 
 
 
26
  return text
27
 
28
  def process_file(file):
29
  extension = os.path.splitext(file.name)[1].lower()
30
-
31
  if extension == '.pdf':
32
  return extract_pdf_text(file)
33
- elif extension == '.xlsx' or extension == '.xls':
34
  return extract_excel_text(file)
35
- elif extension == '.html' or extension == '.htm':
36
  return extract_html_text(file)
 
 
37
  else:
38
  return "Unsupported file format."
39
 
40
- # Streamlit app interface
41
  st.title("File Content Extractor")
42
 
43
- # Streamlit file uploader widget
44
- uploaded_file = st.file_uploader("Choose a file", type=["pdf", "xlsx", "xls", "html", "htm"])
45
 
46
  if uploaded_file is not None:
47
- # Process the file and extract content
48
- file_content = process_file(uploaded_file)
49
-
50
- st.subheader("Extracted Content")
51
- st.text(file_content)
 
1
  import PyPDF2
2
  import openpyxl
3
  from bs4 import BeautifulSoup
 
4
  import os
5
+ import streamlit as st
6
 
7
  def extract_pdf_text(pdf_file):
8
+ with open(pdf_file, 'rb') as file:
9
+ reader = PyPDF2.PdfReader(file)
10
+ text = ''
11
+ for page in reader.pages:
12
+ text += page.extract_text()
13
  return text
14
 
15
  def extract_excel_text(excel_file):
 
22
  return text
23
 
24
  def extract_html_text(html_file):
25
+ with open(html_file, 'r', encoding='utf-8') as file:
26
+ soup = BeautifulSoup(file, 'html.parser')
27
+ text = soup.get_text() # Extract visible text
28
+ return text
29
+
30
+ def extract_txt_text(txt_file):
31
+ with open(txt_file, 'r', encoding='utf-8') as file:
32
+ text = file.read() # Read entire text file
33
  return text
34
 
35
  def process_file(file):
36
  extension = os.path.splitext(file.name)[1].lower()
37
+
38
  if extension == '.pdf':
39
  return extract_pdf_text(file)
40
+ elif extension in ['.xlsx', '.xls']:
41
  return extract_excel_text(file)
42
+ elif extension in ['.html', '.htm']:
43
  return extract_html_text(file)
44
+ elif extension == '.txt':
45
+ return extract_txt_text(file)
46
  else:
47
  return "Unsupported file format."
48
 
49
+ # Streamlit application
50
  st.title("File Content Extractor")
51
 
52
+ uploaded_file = st.file_uploader("Choose a file", type=['pdf', 'xlsx', 'xls', 'html', 'htm', 'txt'])
 
53
 
54
  if uploaded_file is not None:
55
+ # Process the uploaded file
56
+ content = process_file(uploaded_file)
57
+ st.subheader("Extracted Content:")
58
+ st.text(content) # Display extracted content