Skip to content
Shashwat Tiwari edited this page Jan 15, 2025 · 2 revisions

Teacher Timetable Generator

Project Overview

The Teacher Timetable Generator is a Python-based application designed to automate the creation of school timetables. It assigns teachers to periods while ensuring that various constraints are met, such as limiting the number of periods per teacher, avoiding consecutive periods for the same teacher, and ensuring that each subject is taught at least once per day. The generated timetable is saved as an Excel file.


Key Features:

  • Random Teacher Assignment: Assigns teachers to periods randomly while considering the constraints.
  • Teacher Absence Handling: Allows input of absent teachers and excludes them from the timetable.
  • Excel Output: Outputs the generated timetable to an Excel file for easy access and visualization.
  • Color Coding: Each teacher's name is color-coded based on the subject they teach.
  • Constraints on Teacher Assignments:
    • No teacher exceeds a maximum number of periods per day.
    • Teachers are not assigned to consecutive periods in the same class.
    • Teachers are not assigned to multiple classes during the same period.

Code Walkthrough

  1. Reading Teacher Data: The program reads the teacher data from a text file (teachers.txt), where each line contains the teacher’s name and their respective subject.

    teacher_data = {}
    with open("teachers.txt", 'r') as teachers_data_file:
        for line in teachers_data_file:
            line = line.strip("()\n")
            name, subject = line.split("(")
            teacher_data[name.strip()] = subject.strip()
  2. Handling Teacher Absence: The program asks the user to input the names of absent teachers. These teachers are then excluded from the timetable generation.

    absent_teacher = input("Enter names of absent teachers (separate by commas if many): ").split(",")
    absent_teacher = [name.strip() for name in absent_teacher]
    teachers = [teacher for teacher in teachers if teacher not in absent_teacher]
  3. Color Generation: The program generates random light colors to color-code subjects and teachers.

    def generate_light_color():
        r = random.randint(200, 255)
        g = random.randint(200, 255)
        b = random.randint(200, 250)
        return f"{r:02X}{g:02X}{b:02X}"
  4. Teacher Assignment Logic: The assign_teacher function ensures that teachers are assigned while meeting the following conditions:

    • The teacher does not exceed the maximum allowed periods.
    • The teacher is not assigned to consecutive periods in the same class.
    • The teacher is not assigned to multiple classes in the same period.
    • The teacher does not already have an assigned period in the current class.
    def assign_teacher(class_name, period, used_teachers):
        random.shuffle(teachers)
        for teacher in teachers:
            subject = teacher_data[teacher]
            if (
                teacher_period_count[teacher] < max_periods_per_teacher
                and (period == 0 or timetable[class_name][period - 1] != teacher)
                and period not in teacher_assigned_periods[teacher]
                and class_subject_count[class_name][subject] < 1
                and teacher not in used_teachers
            ):
                return teacher
        return None
  5. Excel Output: The timetable is saved to an Excel file using the openpyxl module. The timetable includes the assigned teacher for each period and class, with each teacher color-coded based on their subject.

    workbook = openpyxl.Workbook()
    sheet = workbook.active
    sheet.title = "Timetable"
    # Defining styles
    border = Border(
        left=Side(style="thin"), right=Side(style="thin"),
        top=Side(style="thin"), bottom=Side(style="thin")
    )
    header_fill = PatternFill(start_color="ADD8E6", end_color="ADD8E6", fill_type="solid")
    # Writing header row
    sheet["A1"] = "Period/Class"
    # Filling timetable data
    for period in range(num_periods):
        sheet[f"A{period + 2}"] = f"Period {period + 1}"
        for class_num, class_name in enumerate(classes):
            teacher = timetable[class_name][period]
            col = get_column_letter(class_num + 2)
            cell = sheet[f"{col}{period + 2}"]
            cell.value = teacher
            if teacher:
                cell.fill = PatternFill(start_color=teacher_colors[teacher], fill_type="solid")
    workbook.save("timetable.xlsx")

Technical Specifications

  • Language: Python 3
  • Modules Used:
    • openpyxl: For creating and writing to Excel files.
    • random: For random assignments of teachers to periods.
    • openpyxl.styles: For customizing the appearance of the timetable (e.g., borders, colors, alignment).
  • Input:
    • A text file (teachers.txt) containing teacher names and subjects in the format (name) (subject).
    • User input for absent teachers.
  • Output:
    • An Excel file (timetable.xlsx) with the generated timetable.

Requirements

  1. Python 3.x
  2. Install openpyxl module:
    pip install openpyxl
    

Running the Project

  1. Ensure that the teachers.txt file is in the same directory as the Python script.
  2. Run the Python script:
    python timetable_generator.py
    
  3. Enter the names of any absent teachers when prompted.
  4. The timetable will be generated and saved as timetable.xlsx in the same directory.

Future Improvements

  • Implement a web-based interface for easier user interaction.
  • Add more advanced constraints (e.g., teacher preferences, classroom management).
  • Allow dynamic handling of classes, periods, and subjects.

Feel free to modify and contribute to the project! Thanks for giving you Precious Time!!!!