You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

excel_update.py 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import openpyxl
  2. from openpyxl.styles import PatternFill
  3. import pandas as pd
  4. def fill_cell(ws, cell, color):
  5. fill = PatternFill(start_color=color, end_color=color, fill_type='solid')
  6. cell.fill = fill
  7. def calculate_and_fill_deviation(ws, row, absolute_deviations):
  8. if absolute_deviations:
  9. average_deviation = sum(absolute_deviations) / len(absolute_deviations)
  10. deviation_cell = ws.cell(row=row[0].row, column=4) # Angenommen, die 'Abweichung'-Spalte ist Spalte D
  11. deviation_cell.value = average_deviation
  12. # Färbe die Zelle basierend auf der durchschnittlichen Abweichung
  13. if average_deviation < 5:
  14. fill_color = 'FF00FF00' # Grün
  15. elif 5 <= average_deviation < 10:
  16. fill_color = 'FFFFFF00' # Gelb
  17. else:
  18. fill_color = 'FFFF0000' # Rot
  19. fill_cell(ws, deviation_cell, fill_color)
  20. def color_cells_based_on_deviation(testruns_excel_file_path, testcases_excel_file_path):
  21. wb_testruns = openpyxl.load_workbook(testruns_excel_file_path)
  22. ws_testruns = wb_testruns.active
  23. df_testcases = pd.read_excel(testcases_excel_file_path)
  24. for row in ws_testruns.iter_rows(min_row=2, max_row=ws_testruns.max_row):
  25. deviations = []
  26. absolute_deviations = []
  27. for cell in row[4:]:
  28. header_cell_value = ws_testruns.cell(row=1, column=cell.column).value
  29. if header_cell_value and "Testcase" in header_cell_value:
  30. testcase_num = int(header_cell_value.split('_')[1])
  31. expected_pulse_row = df_testcases[df_testcases['Testcase'] == testcase_num]
  32. if not expected_pulse_row.empty:
  33. expected_pulse = expected_pulse_row.iloc[0]['Puls']
  34. actual_pulse = cell.value
  35. if actual_pulse is not None and expected_pulse is not None:
  36. relative_deviation = (actual_pulse - expected_pulse) / expected_pulse * 100
  37. absolute_deviation = abs(relative_deviation)
  38. deviations.append(relative_deviation)
  39. absolute_deviations.append(absolute_deviation)
  40. if absolute_deviation < 5:
  41. fill_color = 'FF00FF00' # Grün
  42. elif 5 <= absolute_deviation < 10:
  43. fill_color = 'FFFFA500' if relative_deviation < 0 else 'FFFFFF00' # Orange für niedriger, Gelb für höher
  44. else:
  45. fill_color = 'FFC0CB' if relative_deviation < 0 else 'FFFF0000' # Rosa für niedriger, Rot für höher
  46. fill_cell(ws_testruns, cell, fill_color)
  47. calculate_and_fill_deviation(ws_testruns, row, absolute_deviations)
  48. wb_testruns.save(testruns_excel_file_path)