Using Python to download submitted assignments from Moodle

UPDATE : There are two version of this script. Output of both is same but they work on different version of moodle. This script (with configuration file available inside the repo) should work with new moodle while this one (with this configuration) should work with old moodle.
Downloading each student’s assignment from moodle could be a real pain. I’ve updated my old scripts which download assignments from Moodle and stores each student’s assignment in a separate directory named after student. In your home folder .moodlerc file must exists with all details.
These scripts depends on various python-libraries; most of them are in standard library. However, you need to install mechanize.
A few thing about configuration file
Each line (not suffixed with char #) is configuration variable in key = value format. The fields username and password are usual moodle username and password. If you leave password field empty then you have to submit it during execution.
Next is course which should be a regular pattern i.e. if course is EE 705 : V L S I Design Lab then course = EE 705 would be sufficient. Notice that you do not have to enclose any text string using commas etc. If you have two courses with same regular pattern then only first of them will be selected. Then we have activities which should be set to Assignments (open your moodle course page and see what is exact spelling in your course page for assignment, casing of characters matters here.) New moodle gives each assignment an unique name.
[This is only for old script] Once you will click on assignments, there would be more than one assignments, all of them can be put using activity = line. For instance, in sample file Lab session 1 and Lab session 2 are two assignments. It will take care of the rest. It will go to each student, create a directory in his/her name and download submission into that directory. If extract=true then it will also extract archive files. Make sure only one archive file is submitted per assignment.
PS : Some moodle server does not allow robots and automatic logging. To overcome this, this scripts sends wrong information to moodle-server. It tells it that it is "Mozilla Firefox on Ubuntu".

0 thoughts on “Using Python to download submitted assignments from Moodle”

  1. Have you considered making it possible for your script to automatically upload a batch of grades and comments? E.g. you could create a CSV file containing all of these, and then have the script upload them all for a particular activity.

    1. These scripts were for old moodle. The moodle we are using now at my university now allows downloading all submitted assignment as a one zip file. I wrote a script which unzip this file to separate directory named after each students.
      In each directory, One can create a file containing marks and feedback. A script can parse this file and create a csv. I have a script which does it. I’ll share it with you after fixing the format of feedback file.
      They are unplolished : https://github.com/dilawar/Scripts . Once scripts are ready, I’ll let you know and share them on github.

  2. Hi, any idea how to make a python script to download the excel of the gradebook export? I have actually created it but the excel comes out with all the names and details of the students, but without the grades. I have tried to set the value of checkboxes to 1 (at least of the checkbox for course total) but I am still not getting any column with a grade.
    I am using mechanize.
    br.form.set_value(‘1′, name=’itemids[73788]’, nr = 0)
    This seems to work, it gives the value 1 to the checkbox with name itemids[73788]. In this particular case 73788 is the number of the checkbox of the course total for this particular course.
    But still no results.
    Best,

    1. Sorry Fabian, I do not have a working moodle setup to test the script.
      If you can create one login account for me on your moodle, I can give it a try.

Leave a Reply

Scroll to Top