moodle_docs / app.py
alexkueck's picture
Create app.py
8314c51 verified
raw
history blame
1.31 kB
import os
import json
from PyPDF2 import PdfReader
# Funktion zum Extrahieren von Text aus PDF-Dateien
def extract_text_from_pdf(pdf_path):
reader = PdfReader(pdf_path)
text = ""
for page in reader.pages:
text += page.extract_text()
return text
# Funktion zum Extrahieren von Text aus TXT-Dateien
def extract_text_from_txt(txt_path):
with open(txt_path, "r", encoding="utf-8") as file:
return file.read()
# Pfad zu den Dateien im Hugging Face Space
pdf_files = ["doc1.pdf", "doc2.pdf", "doc3.pdf", "doc4.pdf", "doc5.pdf"]
txt_files = ["doc6.txt", "doc7.txt", "doc8.txt"]
# Liste zur Speicherung der Dokumente
documents = []
# PDF-Dateien verarbeiten
for pdf_file in pdf_files:
if os.path.exists(pdf_file):
content = extract_text_from_pdf(pdf_file)
documents.append({"id": len(documents) + 1, "content": content})
# TXT-Dateien verarbeiten
for txt_file in txt_files:
if os.path.exists(txt_file):
content = extract_text_from_txt(txt_file)
documents.append({"id": len(documents) + 1, "content": content})
# Dokumente in eine JSON-Datei speichern
with open("documents.json", "w", encoding="utf-8") as json_file:
json.dump(documents, json_file, ensure_ascii=False, indent=4)
print("documents.json wurde erfolgreich erstellt.")