Diese im selben Ordner wie das Skript und die CSV abspeichern.
import pandas as pd
import json
import zipfile
import shutil
import os
# === Pfade definieren ===
csv_path = "Eingabe Advanced Fill in the Blanks - Tabelle.CSV"
h5p_vorlage = "vorlage.h5p"
neue_h5p = "mein_output.h5p"
temp_dir = "temp_h5p"
# === Schritt 1: CSV einlesen und content.json erzeugen ===
df = pd.read_csv(csv_path, sep=';', encoding='utf-8')
df.columns = df.columns.str.strip()
def create_blanks_text(s):
return s.replace("*", "____")
content = {
"blanksText": "",
"blanksList": []
}
blanks = []
texts = []
for _, row in df.iterrows():
if pd.isna(row['Satz']):
continue
texts.append(f"<p>{create_blanks_text(row['Satz'])}</p>")
correct = row['Lösung']
hint = row.get('Tipp', None)
incorrects = []
for i in range(1, 5):
wrong = row.get(f'Vom Nutzer eingegebene falsche Antwort {i}')
feedback = row.get(f'Rückmeldung.{i-1}')
if pd.notna(wrong):
incorrects.append({
"showHighlight": False,
"highlight": "-1",
"incorrectAnswerText": wrong,
"incorrectAnswerFeedback": f"<div>{feedback or ''}</div>"
})
entry = {
"correctAnswerText": correct,
"incorrectAnswersList": incorrects
}
if pd.notna(hint):
entry["hint"] = hint
blanks.append(entry)
content["blanksText"] = "\n".join(texts)
content["blanksList"] = blanks
full_data = {
"content": content,
"behaviour": {
"mode": "selection",
"selectAlternatives": "alternatives",
"selectAlternativeRestriction": 1,
"spellingErrorBehaviour": "mistake",
"caseSensitive": False,
"autoCheck": False,
"enableSolutionsButton": False,
"showSolutionsRequiresInput": True,
"enableRetry": True,
"enableCheckButton": True
},
"showSolutions": "Lösung anzeigen",
"tryAgain": "Wiederholen",
"checkAnswer": "Überprüfen",
"notFilledOut": "Bitte fülle alle Lücken aus, um die Lösung zu sehen",
"tipLabel": "Tipp",
"scoreBarLabel": "Du hast :num von :total Punkten erreicht."
}
# === Schritt 2: Vorlage entpacken ===
if os.path.exists(temp_dir):
shutil.rmtree(temp_dir)
with zipfile.ZipFile(h5p_vorlage, 'r') as zip_ref:
zip_ref.extractall(temp_dir)
# === Schritt 3: content.json ersetzen ===
with open(os.path.join(temp_dir, "content", "content.json"), "w", encoding="utf-8") as f:
json.dump(full_data, f, ensure_ascii=False, indent=2)
# === Schritt 4: Alles wieder einpacken ===
with zipfile.ZipFile(neue_h5p, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, _, files in os.walk(temp_dir):
for file in files:
abs_path = os.path.join(root, file)
rel_path = os.path.relpath(abs_path, temp_dir)
zipf.write(abs_path, rel_path)
print("✅ Neue H5P-Datei erfolgreich erstellt:", neue_h5p)
Das Skript in einen Texteditor einfügen und mit der Dateiendung .py im gemeinsamen Ordner mit der CSV-Datei und der H5P-Vorlage abspeichern.
Durch einen Doppelklick wird das Skript ausgeführt und die neue H5P-Datei erstellt.