ackermanmoriii / python-fundamentals

0 stars 0 forks source link

Hgh #34

Open ackermanmoriii opened 2 months ago

ackermanmoriii commented 2 months ago
import os
from docx import Document

def extract_student_name(text):
    """
    This function 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):
    """
    This function 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 = "path/to/source/folder"  # Update this with the path to your source folder
    destination_folder = "path/to/destination/folder"  # Update this with the path to your destination folder

    # Process the Word files
    process_word_files(source_folder, destination_folder)