2023-01-25 09:58:13 +00:00
|
|
|
#
|
|
|
|
# Voraussetzungen:
|
|
|
|
#
|
|
|
|
# pip install xlrd
|
|
|
|
# pip install sqlite3
|
|
|
|
#
|
|
|
|
# oder unter z. B. Ubuntu: apt install python3-xlrd python3-sqlite3
|
|
|
|
#
|
|
|
|
|
2023-01-25 09:00:02 +00:00
|
|
|
import sqlite3
|
2023-01-25 15:10:13 +00:00
|
|
|
import xlrd
|
2023-01-25 09:00:02 +00:00
|
|
|
|
|
|
|
SQLITE_FILE = "database.db"
|
2023-01-25 15:10:13 +00:00
|
|
|
connection = sqlite3.connect(SQLITE_FILE)
|
|
|
|
|
|
|
|
#con.execute("create table pruefungsergebnis (pruefung_id int not null, "\
|
|
|
|
# + "mtknr int not null, note decimal(3,1), primary key(pruefung_id, mtknr))")
|
2023-01-25 09:00:02 +00:00
|
|
|
#con.commit()
|
|
|
|
|
2023-01-25 15:10:13 +00:00
|
|
|
# Hier als Konstante, da in der Praxis wahrscheinlich aus Kontext
|
|
|
|
# in Webanwendung abgeleitet
|
2023-01-25 09:14:55 +00:00
|
|
|
PRUEFUNG_ID = 3
|
2023-01-25 09:00:02 +00:00
|
|
|
|
|
|
|
def read_file(filename):
|
|
|
|
# alle Validierungsschritte zugunsten eines übersichtlichen Beispiels eingespart
|
2023-01-25 15:10:13 +00:00
|
|
|
xlfile = xlrd.open_workbook(filename)
|
|
|
|
xlsheet = xlfile.sheet_by_index(0)
|
2023-01-25 09:00:02 +00:00
|
|
|
print(f"Importiere aus {filename} - {xlsheet.name}")
|
|
|
|
|
|
|
|
data = {
|
|
|
|
int(xlsheet.cell(rx, 0).value): xlsheet.cell(rx, 1).value
|
2023-01-25 15:10:13 +00:00
|
|
|
for rx in range(1, xlsheet.nrows)
|
2023-01-25 09:00:02 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
def is_already_set(con, pruefung_id, mtknr):
|
|
|
|
sql = "select count(*) as anzahl from pruefungsergebnis where pruefung_id = ? and mtknr = ?"
|
|
|
|
cur = con.cursor()
|
|
|
|
cur.execute(sql, (pruefung_id, mtknr))
|
|
|
|
result = cur.fetchone()[0]
|
|
|
|
return result >= 1
|
|
|
|
|
|
|
|
|
2023-01-25 09:58:13 +00:00
|
|
|
def is_valid_value(pruefung_id, note):
|
2023-01-25 12:23:56 +00:00
|
|
|
# Im realen System z. B. auf der Basis der Notengebungsart etc. prüfen
|
2023-01-25 15:10:13 +00:00
|
|
|
return note is not None and note in [
|
|
|
|
1, 1.3, 1.7, 2, 2.3, 2.7, 3, 3.3, 3.7, 4, 5
|
|
|
|
]
|
2023-01-25 09:58:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
def is_modified(con, pruefung_id, mtknr, note):
|
2023-01-25 15:10:13 +00:00
|
|
|
sql = "select count(*) as anzahl from pruefungsergebnis" \
|
|
|
|
+ " where pruefung_id = ? and mtknr = ? and note = ?"
|
2023-01-25 09:58:13 +00:00
|
|
|
cur = con.cursor()
|
|
|
|
cur.execute(sql, (pruefung_id, mtknr, note))
|
|
|
|
result = cur.fetchone()[0]
|
|
|
|
return result == 0
|
|
|
|
|
|
|
|
|
2023-01-25 09:14:55 +00:00
|
|
|
def merge_data(pruefung_id, new_data):
|
2023-01-25 09:00:02 +00:00
|
|
|
update_sql = "update pruefungsergebnis set note = ? where pruefung_id = ? and mtknr = ?"
|
|
|
|
insert_sql = "insert into pruefungsergebnis (pruefung_id, mtknr, note) values (?, ?, ?)"
|
2023-01-25 15:10:13 +00:00
|
|
|
# Transaktionsscope: Fehler führt zuverlässig zum Verwerfen
|
|
|
|
# aller Änderungen aus einer Excel-Datei
|
|
|
|
with connection:
|
2023-01-25 09:00:02 +00:00
|
|
|
for mtknr in new_data.keys():
|
|
|
|
note = new_data[mtknr]
|
2023-01-25 15:10:13 +00:00
|
|
|
if is_already_set(connection, pruefung_id, mtknr):
|
2023-01-25 12:23:56 +00:00
|
|
|
if is_valid_value(pruefung_id, note):
|
2023-01-25 15:10:13 +00:00
|
|
|
if is_modified(connection, pruefung_id, mtknr, note):
|
2023-01-25 12:23:56 +00:00
|
|
|
print(f"Wollen Sie die Note zu {mtknr} auf {note} aktualisieren? (j/n)")
|
|
|
|
answer = input()
|
|
|
|
if answer == 'j':
|
2023-01-25 15:10:13 +00:00
|
|
|
connection.execute(update_sql, (note, pruefung_id, mtknr))
|
|
|
|
print("angepasst")
|
2023-01-25 12:23:56 +00:00
|
|
|
else:
|
2023-01-25 15:10:13 +00:00
|
|
|
print("unverändert")
|
2023-01-25 09:58:13 +00:00
|
|
|
else:
|
2023-01-25 12:23:56 +00:00
|
|
|
print(f"Note zu {mtknr} wird nicht geändert, Notenwert unverändert")
|
2023-01-25 09:00:02 +00:00
|
|
|
else:
|
2023-01-25 12:23:56 +00:00
|
|
|
print(f"Note zu {mtknr} wird nicht geändert, Zeile leer oder ungültig")
|
2023-01-25 09:00:02 +00:00
|
|
|
else:
|
2023-01-25 12:23:56 +00:00
|
|
|
print(f"Mtknr: {mtknr} Note: {note} hinzufügen")
|
2023-01-25 09:00:02 +00:00
|
|
|
# im Falle von HISinOne wegen bereits bestehendem Anmeldesatz auch Update, so aber
|
|
|
|
# einfacher im Beispiel darzustellen...
|
2023-01-25 15:10:13 +00:00
|
|
|
connection.execute(insert_sql, (pruefung_id, mtknr, note))
|
2023-01-25 09:00:02 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
f1 = read_file("excel_file_1.xlsx")
|
2023-01-25 09:14:55 +00:00
|
|
|
merge_data(PRUEFUNG_ID, f1)
|
2023-01-25 09:00:02 +00:00
|
|
|
|
|
|
|
f2 = read_file("excel_file_2.xlsx")
|
2023-01-25 09:14:55 +00:00
|
|
|
merge_data(PRUEFUNG_ID, f2)
|