ackermanmoriii / python-fundamentals

0 stars 0 forks source link

Aaaw #35

Open ackermanmoriii opened 1 month ago

ackermanmoriii commented 1 month ago
import os
from docx import Document

def extract_student_name(text):
    """
    Extracts the student name between "نام دانشجو:" and the next period ".".
    """
    start_marker = "نام دانشجو:"
    end_marker = "."

    try:
        start_idx = text.index(start_marker) + len(start_marker)
        end_idx = text.index(end_marker, start_idx)
        student_name = text[start_idx:end_idx].strip()
        return student_name
    except ValueError:
        return None

def process_word_files(source_folder, destination_folder):
    """
    Processes all Word files in the source folder, extracts student names, 
    and moves files to new folders named after the extracted student names.
    """
    if not os.path.exists(destination_folder):
        os.makedirs(destination_folder)

    for filename in os.listdir(source_folder):
        if filename.endswith(".docx"):
            filepath = os.path.join(source_folder, filename)
            try:
                doc = Document(filepath)
                full_text = []
                for para in doc.paragraphs:
                    full_text.append(para.text)
                combined_text = "\n".join(full_text)

                student_name = extract_student_name(combined_text)
                if student_name:
                    # Create a new folder named after the student
                    student_folder = os.path.join(destination_folder, student_name)
                    if not os.path.exists(student_folder):
                        os.makedirs(student_folder)

                    # Move the Word file to the new folder
                    new_filepath = os.path.join(student_folder, filename)
                    os.rename(filepath, new_filepath)
                    print(f"Moved {filename} to {student_folder}")
                else:
                    print(f"Could not find a student name in {filename}")
            except Exception as e:
                print(f"Error processing file {filename}: {e}")

if __name__ == "__main__":
    # Define source and destination folders
    source_folder = "source_docs"
    destination_folder = "organized_docs"

    # Process the Word files
    process_word_files(source_folder, destination_folder)