Commit a421165a authored by Olivier's avatar Olivier
Browse files

Module 10: process grades

The code in this commit is contributed by Germán Santa Maria.
parent 09401a28
# -*- coding: utf-8 -*-
"""
Author: Germán Santa Maria
Script: Process Grades
Description:
This script will read all pdf files in the folder where is contained and
will produce a CSV file with the values extracted from the all fillable
text boxes in each of the pdfs.
Conditions:
All the pdfs should come from the same form template (same number of
fillable text boxes).
"""
"""-------------------------Hard coded input--------------------------------"""
# Output file name
csv_file_name = 'grades.csv' # Output csv file name (to store grades)
"""-------------------------------------------------------------------------"""
# Import modules
import os
import PyPDF2 # For working with PDFs
import csv # For reading cvs files
# List of all the files in the directory
list_of_files = os.listdir()
# Filter only ".pdf" files
list_of_pdf_files = []
for i in range(len(list_of_files)) :
if list_of_files[i].endswith(".pdf"):
list_of_pdf_files.append(list_of_files[i])
#---------------Create the headers for the output csv file--------------------#
# Select first pdf in the list as calibration pdf
calibration_pdf = list_of_pdf_files[0]
# Read PDF file
object = open(calibration_pdf, 'rb') # rb means read binary
reader = PyPDF2.PdfFileReader(object)
# Get values from text boxes in the PDF
text_boxes_data = reader.getFormTextFields()
# Create headers for the csv file
headers = []
headers.append('pdf_name')
text_boxes_names = list(text_boxes_data.keys())
for i in range(len(text_boxes_names)):
headers.append(str(text_boxes_names[i]))
#------------------Extract grades from all the pdf files----------------------#
line_data = [] # Array with the data to be printed in each line of the CSV file
errors = [] # List of pdfs that could not be processed
for pdf_file in list_of_pdf_files:
try:
# Read PDF file
object = open(pdf_file, 'rb') # rb means read binary
reader = PyPDF2.PdfFileReader(object)
# Get values from text boxes in the PDF
text_boxes_data = reader.getFormTextFields()
# Get grades for the student
student_grades = []
for i in range(len(text_boxes_names)):
grade = text_boxes_data.get(text_boxes_names[i], "")
student_grades.append(str(grade))
# Add pdf file-name and the student's grades to the "line_data" array
line = []
line.append(pdf_file)
for j in range (len(student_grades)):
line.append(student_grades[j])
line_data.append(line)
except:
# Add pdf file-name and the student's grades to the "line_data" array
print("pdf file " + pdf_file + "could not be processed.")
errors.append(pdf_file)
#------------------------Store grades in csv file-----------------------------#
with open (csv_file_name,'w',newline='') as f:
writer = csv.writer(f)
writer.writerow(headers)
for i in range(0,len(line_data)):
writer.writerow(line_data[i])
if len(errors) > 0:
with open ("errors.csv",'w',newline='') as f:
writer = csv.writer(f)
for i in range(0,len(errors)):
writer.writerow([errors[i]])
# End script
#!/bin/bash
# Input parameters
inputfolder='script_input-real'
outputfolder='script_output-real'
timestamp=$(date "+%Y-%m-%d+%H%M%S")
outputbackupname="backup_output_$timestamp.tar"
inputbackupname="backup_input_$timestamp.tar"
# Create archive of output folder
mkdir -vp archives
tar -cvf $outputbackupname --exclude=old $outputfolder
mv -vf $outputbackupname archives/
tar -cvf $inputbackupname --exclude=old $inputfolder
mv -vf $inputbackupname archives/
# Import output of M9 and M6 into input of M10
rsync -avh --delete ../M9_collect_graded/pdfs/ $inputfolder/pdfs/
rsync -avh --delete ../M6_match_peers/script_output/peers.csv $inputfolder/
# Clear output folder
rm -rvf $outputfolder/*
echo "end of script."
......@@ -21,7 +21,7 @@ The code in this repository is published under the [GNU GPL v3](href="https://ww
## Structure
A total of ten modules (eight of which being locally-run scripts) are needed to run the project.
A total of 11 modules (8 of which being locally-run scripts) are needed to run the project.
[ ] Module 1: Collect participant information
(through email)
......@@ -47,8 +47,11 @@ A total of ten modules (eight of which being locally-run scripts) are needed to
[x] [Module 8](M8_send_grading): Send peer grading assignments
(in Python)
[ ] Module 9: Process grades
[ ] Module 9: Collect peer-assigned grades
(through email)
[x] [Module 10](M10_process_grades): Process grades
(in Python)
[ ] Module 10: Send results to students
[ ] Module 11: Send results to students
(in Python)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment