-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
93 lines (74 loc) · 2.16 KB
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import json
import csv
import pandas as pd
import docx
from PyPDF2 import PdfReader
from pptx import Presentation
from bs4 import BeautifulSoup
from lxml import etree
from openpyxl import load_workbook
"""
In this file Factory Design Pattern is used to make this functionalities more flexible & maintainable.
Currently there are main file handlers for PDF, DOCX, TXT, PPTX, XLSX, CSV, HTML, XML, JSON.
"""
def read_pdf(file):
try:
reader = PdfReader(file)
return [page.extract_text() for page in reader.pages]
except Exception as e:
print(f"Error reading Pdf file: {e}")
return
def read_docx(file):
try:
doc = docx.Document(file)
return [para.text for para in doc.paragraphs]
except Exception as e:
print(f"Error reading Docx file: {e}")
return
def read_txt(file):
try:
return file.read().splitlines()
except Exception as e:
print(f"Error reading Txt file: {e}")
return
def read_pptx(file):
try:
pres = Presentation(file)
return [slide.notes_slide.text for slide in pres.slides if slide.notes_slide]
except Exception as e:
print(f"Error reading pptx file: {e}")
return
def read_xlsx(file):
try:
reader = pd.read_excel(file)
return [" ".join(row) for row in reader]
except Exception as e:
print(f"Error reading Xlsx file: {e}")
return
def read_csv(file):
try:
reader = csv.reader(file)
return [" ".join(row) for row in reader]
except Exception as e:
print(f"Error reading Csv file: {e}")
return
def read_html(file):
try:
soup = BeautifulSoup(file, "html.parser")
return soup.get_text()
except Exception as e:
print(f"Error reading Html file: {e}")
return
def read_xml(file):
try:
tree = etree.parse(file)
return etree.tostring(tree.getroot(), pretty_print=True)
except Exception as e:
print(f"Error reading Xml file: {e}")
return
def read_json(file):
try:
return json.load(file)
except Exception as e:
print(f"Error reading Json file: {e}")
return