78 lines
3.4 KiB
Python
78 lines
3.4 KiB
Python
|
import pandas as pd
|
||
|
|
||
|
def read_excel_file(file_path):
|
||
|
"""Read the Excel file and return a DataFrame."""
|
||
|
try:
|
||
|
return pd.read_excel(file_path)
|
||
|
except Exception as e:
|
||
|
print(f"Error reading the Excel file: {e}")
|
||
|
return None
|
||
|
|
||
|
def filter_studiegidsnummer(df):
|
||
|
"""Filter rows where 'studiegidsnummer' contains 'GES'."""
|
||
|
if 'Studiegidsnummer' not in df.columns:
|
||
|
print("Column 'studiegidsnummer' not found in the DataFrame.")
|
||
|
print("Available columns:", df.columns)
|
||
|
return pd.DataFrame() # Return an empty DataFrame
|
||
|
return df[df['Studiegidsnummer'].str.contains('GES', na=False)].copy()
|
||
|
|
||
|
def filter_opmerkingen(df):
|
||
|
"""Filter rows where 'Opmerkingen' does NOT contain '24-25'."""
|
||
|
if 'Opmerkingen' not in df.columns:
|
||
|
print("Column 'Opmerkingen' not found in the DataFrame.")
|
||
|
print("Available columns:", df.columns)
|
||
|
return pd.DataFrame() # Return an empty DataFrame
|
||
|
return df[~df['Opmerkingen'].str.contains('24-25', na=False)].copy()
|
||
|
|
||
|
def create_message_column(df):
|
||
|
"""Create 'Message' and 'subject' columns with the specified format."""
|
||
|
df.loc[:, 'Message'] = df.apply(lambda row: (
|
||
|
f"Beste docent,\n\n"
|
||
|
f"Ik ben de examengegevens aan het controleren van {row['Omschrijving']} {row['Studiegidsnummer']}. De huidige gegevens zijn als volgt:\n\n"
|
||
|
f"{row['Examenvorm']} examen voor zowel eerste als tweede zit, {row['Examenduur']} minuten, tussen {row['Beginuur voormiddag']} en {row['Einduur voormiddag']} of {row['Beginuur namiddag']} en {row['Einduur namiddag']}.\n\n"
|
||
|
f"Gelden dezelfde gegevens voor dit academiejaar of moeten er nog wijzigingen doorgevoerd worden? Alvast dank voor je reactie!"
|
||
|
), axis=1)
|
||
|
df.loc[:, 'subject'] = df.apply(lambda row: (
|
||
|
f"Examengegevens {row['Omschrijving']} {row['Studiegidsnummer']}"
|
||
|
), axis=1)
|
||
|
return df
|
||
|
|
||
|
def save_to_excel(df, output_file_path):
|
||
|
"""Save the DataFrame to a new Excel file."""
|
||
|
try:
|
||
|
df.to_excel(output_file_path, index=False)
|
||
|
except Exception as e:
|
||
|
print(f"Error saving the Excel file: {e}")
|
||
|
|
||
|
def convert_time_format(time_str):
|
||
|
"""Convert time from 'HH:MM:SS' to 'HH:MM'."""
|
||
|
try:
|
||
|
return pd.to_datetime(time_str).strftime('%H:%M')
|
||
|
except Exception as e:
|
||
|
print(f"Error converting time format: {e}")
|
||
|
return time_str
|
||
|
|
||
|
def apply_time_format_conversion(df, columns):
|
||
|
"""Apply time format conversion to specified columns in the DataFrame."""
|
||
|
for column in columns:
|
||
|
df[column] = pd.to_datetime(df[column], format='%H:%M:%S', errors='coerce').dt.strftime('%H:%M')
|
||
|
return df
|
||
|
|
||
|
# Example usage within the main function
|
||
|
def main():
|
||
|
file_path = 'examengegevens2425.xlsx'
|
||
|
output_file_path = 'filtered_examengegevens2425.xlsx'
|
||
|
|
||
|
df = read_excel_file(file_path)
|
||
|
if df is not None:
|
||
|
filtered_df = filter_studiegidsnummer(df)
|
||
|
if not filtered_df.empty:
|
||
|
final_filtered_df = filter_opmerkingen(filtered_df)
|
||
|
# Convert time format for specified columns
|
||
|
time_columns = ['Beginuur voormiddag', 'Einduur voormiddag', 'Beginuur namiddag', 'Einduur namiddag']
|
||
|
final_filtered_df = apply_time_format_conversion(final_filtered_df, time_columns)
|
||
|
final_filtered_df = create_message_column(final_filtered_df)
|
||
|
save_to_excel(final_filtered_df, output_file_path)
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|