Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import PyPDF2
|
| 2 |
+
import openpyxl
|
| 3 |
+
from bs4 import BeautifulSoup
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
def extract_pdf_text(pdf_file):
|
| 7 |
+
with open(pdf_file, 'rb') as file:
|
| 8 |
+
reader = PyPDF2.PdfReader(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):
|
| 15 |
+
workbook = openpyxl.load_workbook(excel_file)
|
| 16 |
+
sheet = workbook.active
|
| 17 |
+
text = ''
|
| 18 |
+
for row in sheet.iter_rows(values_only=True):
|
| 19 |
+
row_text = ' '.join([str(cell) for cell in row if cell is not None])
|
| 20 |
+
text += row_text + '\n'
|
| 21 |
+
return text
|
| 22 |
+
|
| 23 |
+
def extract_html_text(html_file):
|
| 24 |
+
with open(html_file, 'r', encoding='utf-8') as file:
|
| 25 |
+
soup = BeautifulSoup(file, 'html.parser')
|
| 26 |
+
text = soup.get_text() # Extract visible text
|
| 27 |
+
return text
|
| 28 |
+
|
| 29 |
+
def process_file(file_path):
|
| 30 |
+
extension = os.path.splitext(file_path)[1].lower()
|
| 31 |
+
|
| 32 |
+
if extension == '.pdf':
|
| 33 |
+
return extract_pdf_text(file_path)
|
| 34 |
+
elif extension == '.xlsx' or extension == '.xls':
|
| 35 |
+
return extract_excel_text(file_path)
|
| 36 |
+
elif extension == '.html' or extension == '.htm':
|
| 37 |
+
return extract_html_text(file_path)
|
| 38 |
+
else:
|
| 39 |
+
return "Unsupported file format."
|
| 40 |
+
|
| 41 |
+
# Example usage:
|
| 42 |
+
file_path = input("Enter the file path: ")
|
| 43 |
+
content = process_file(file_path)
|
| 44 |
+
|
| 45 |
+
print("\nExtracted Content:\n")
|
| 46 |
+
print(content)
|