49 lines
1.8 KiB
Python
49 lines
1.8 KiB
Python
import os
|
|
from pypdf import PdfReader
|
|
from docx import Document
|
|
from pptx import Presentation
|
|
|
|
SRC = '/home/maxi/Projekt/wtc.waiswuaschd.de/data/import_2026'
|
|
DEST = '/home/maxi/Projekt/wtc.waiswuaschd.de/data/extracted_texts'
|
|
os.makedirs(DEST, exist_ok=True)
|
|
|
|
for f in sorted(os.listdir(SRC)):
|
|
path = os.path.join(SRC, f)
|
|
ext = os.path.splitext(f)[1].lower()
|
|
text = ""
|
|
try:
|
|
if ext == '.pdf':
|
|
reader = PdfReader(path)
|
|
for i, page in enumerate(reader.pages):
|
|
text += f"\n--- Page {i+1} ---\n" + (page.extract_text() or "")
|
|
elif ext == '.docx':
|
|
doc = Document(path)
|
|
for p in doc.paragraphs:
|
|
if p.text.strip():
|
|
text += p.text.strip() + "\n"
|
|
for t in doc.tables:
|
|
text += "\n--- Table ---\n"
|
|
for row in t.rows:
|
|
row_txt = [c.text.strip() for c in row.cells if c.text.strip()]
|
|
if row_txt:
|
|
text += " | ".join(row_txt) + "\n"
|
|
elif ext == '.pptx':
|
|
prs = Presentation(path)
|
|
for i, slide in enumerate(prs.slides):
|
|
text += f"\n--- Slide {i+1} ---\n"
|
|
for shape in slide.shapes:
|
|
if shape.has_text_frame:
|
|
for paragraph in shape.text_frame.paragraphs:
|
|
if paragraph.text.strip():
|
|
text += paragraph.text.strip() + "\n"
|
|
else:
|
|
text = f"[{ext} file: {f}]"
|
|
except Exception as e:
|
|
text = f"[ERROR: {e}]"
|
|
|
|
out_name = os.path.splitext(f)[0] + ".txt"
|
|
with open(os.path.join(DEST, out_name), 'w', encoding='utf-8') as out_f:
|
|
out_f.write(text)
|
|
|
|
print("Extraction complete. Files written to", DEST)
|