Spaces:
No application file
No application file
Create extractor.py
Browse files- extractor.py +42 -0
extractor.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from pptx import Presentation
|
| 2 |
+
from pypdf import PdfReader
|
| 3 |
+
|
| 4 |
+
|
| 5 |
+
def extract_ppt_text(file):
|
| 6 |
+
|
| 7 |
+
prs = Presentation(file)
|
| 8 |
+
|
| 9 |
+
slides_text = []
|
| 10 |
+
|
| 11 |
+
for slide in prs.slides:
|
| 12 |
+
|
| 13 |
+
slide_text = ""
|
| 14 |
+
|
| 15 |
+
for shape in slide.shapes:
|
| 16 |
+
|
| 17 |
+
if hasattr(shape, "text"):
|
| 18 |
+
|
| 19 |
+
slide_text += shape.text + "\n"
|
| 20 |
+
|
| 21 |
+
# avoid empty slides
|
| 22 |
+
if slide_text.strip():
|
| 23 |
+
slides_text.append(slide_text)
|
| 24 |
+
|
| 25 |
+
return slides_text
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
def extract_pdf_text(file):
|
| 29 |
+
|
| 30 |
+
reader = PdfReader(file)
|
| 31 |
+
|
| 32 |
+
pages_text = []
|
| 33 |
+
|
| 34 |
+
for page in reader.pages:
|
| 35 |
+
|
| 36 |
+
text = page.extract_text()
|
| 37 |
+
|
| 38 |
+
if text and text.strip():
|
| 39 |
+
|
| 40 |
+
pages_text.append(text)
|
| 41 |
+
|
| 42 |
+
return pages_text
|