DEV Community

James Shah
James Shah Subscriber

Posted on

How I Automated The Google Form Filling For My College Attendance Using Python

To fight Corona Virus, our Prime Minister has declared lockdown for 21 days in all over the country. And so, our college decided to take online lectures and also, sent us a google form which is to be filled every day for each lecture to fill attendance. Filling form 4 times a day with the subject name and corresponding time and professor details is a tedious task. But also, Attendance is Engineer's Only Motivation to attend lectures.

And So, I decided to use Python and to write a script to automate this tedious task.

As usual, I started by searching on google to see how can we submit the google form using python script. And I came to know that Every Google Form Input element has a "name" attribute attached to it and we can use it to send the data for the corresponding field.

So, I opened the google form in chrome and Started inspecting Input element in which I will be filling the data, and I found a bunch of ss-form-entry objects with name attribute like " name = entry.<id> "

I copied every name attribute value of the field.

Now, In Python I created a dictionary with the value of name attributes that we copied from the browser as keys and the data that I want to fill in the corresponding field as it's value.

Then I created a dictionary with Day Name(Monday, Tuesday, etc.) as keys and the lectures' details as list as values.

And put both the dictionaries in a function that returns the list of different data dictionaries of the current day.

And, finally the function for sending the form response using POST method of requests library. It takes two arguments - URL and data(here, data_dictionary), respectively.

Note: Change the URL of google form by replacing '/viewform' at the end of the URL with 'formResponse'. Now, your URL should look like this https://docs.google.com/forms/d/<form_id>/formResponse

And that's it, now I just have to run this script once a day and it will fill my attendance for all the lectures of that day.
I can also deploy it on a server like PythonAnyWhere or DigitalOcean and it can run by itself every day, but as I'm home quarantine for the 21 Days, I have more than enough time to run it manually every day.🤓🤓

You can find the full code here : AutoFill Google Form

Latest comments (30)

Collapse
 
pokkatsinan profile image
Mohammed Sinan pokkat

I was searching for this for a long time, thanks very much

Collapse
 
sahilcodesmax profile image
sahilcodes-max

I need help. I am trying to use this but for a multiple choice problem. For example, the question is 1 + 1.
The first option is 1, second is 2. How would we do this?
Image of my code: (I basicilly removed everything for a basic one question. )
Url: docs.google.com/forms/d/e/1FAIpQLS...

Collapse
 
sahilcodesmax profile image
sahilcodes-max

For me it says requests does not exist? How do I download it?

Collapse
 
sahilcodesmax profile image
sahilcodes-max

nevermind I downloaded it

Collapse
 
djdragonfruit profile image
DJDragonfruit • Edited

Hi, this is a really great guide! However I'm trying to fill out a google doc that is locked to access by members of a certain organization. Can I make this code automatically sign me into my school account before filling out the form? (I've looked through a gazillion solutions for this on GitHub and none of them seem to work. Chances are I'm using them wrong, but try as I might I can't figure out how.)

Collapse
 
ananyaboop profile image
ananya-boop

Hey, great work! I am trying to follow the same step but as I am a beginner, not really sure how to go about it. I have a form to fill every day and was thinking about how to automate it. It consists of several fields as well. Let me know if you could help in any way :)

Collapse
 
yechielk profile image
Yechiel Kalmenson

Thanks! This is very useful! I came here because I was searching to see if there's some kind of API to submit Google forms (my kids' school also has us submitting forms every day :)

I didn't think it would be as simple as sending a POST request to the /formResponse endpoint 😂

Collapse
 
jamesshah profile image
James Shah

I'm glad that this helped you. 👍😄

Collapse
 
ujjalbaniya profile image
Ujjal-Baniya

I am unable to find entiry.id can you help ?

Collapse
 
jamesshah profile image
James Shah

You can find it from the elements tab in dev tools. It's a pretty cumbersome task to find all the entry is, especially when there are lots of fields. In my opinion, you should take a look at the method suggested by Simon in the comments. It's very easy comparing to my entry id method.

Collapse
 
ujjalbaniya profile image
Ujjal-Baniya

I got that :) okay thanks mate

Collapse
 
dipakp2726 profile image
dipakp2726

its not working in form which has set response receipt always,

Collapse
 
jamesshah profile image
James Shah

No that's not the case. As I've tried it on such form which sends a response receipt.

Collapse
 
kolcun profile image
Mike Kolcun

I'm having the same issue with a test form. If I set "response receipts" to "Always" I only ever get error 400. if I set "response receipts" to "If respondent requests it" I am able to get the form to submit.

Any thoughts?

Collapse
 
dipakp2726 profile image
dipakp2726

try again bro, i have tried today, its working when i disable this option and getting error 400 when i activate it

Thread Thread
 
minzhekang profile image
Kang Min Zhe

The reason why you are getting a response error of 400 is because when using the requests module, the posted data is automatically url encoded. Hence posting options that contains characters such as "+", "@" ..etc will cause a bad form error.

Hence, one way you can work around this is to have the request post http URL instead by passing a string.

string = "entry.xxx=Option+1&entry.xxx=option2"
r = requests.post(url, params = string)

Hope this helps.

Collapse
 
dipakp2726 profile image
dipakp2726

Error occured HTTPSConnectionPool(host='docs.google.com', port=443): Max retries exceeded with url: /forms/d/1JxbxYl7ZnWTEKtYqVMQJOi_6_cZvTYrDbGsPeNNnGSY/formResponse?edit_requested=true (Caused by SSLError("Can't connect to HTTPS URL because the SSL module is not available."))

i am having these error ,

Some comments may only be visible to logged-in visitors. Sign in to view all comments.