Skip to content

Instantly share code, notes, and snippets.

@sebnil
Last active April 29, 2021 21:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save sebnil/588b5b6f864f672b4d22 to your computer and use it in GitHub Desktop.
Save sebnil/588b5b6f864f672b4d22 to your computer and use it in GitHub Desktop.
Convert Google Forms responses into a word document (docx) #script
from docx import Document
from openpyxl import load_workbook
# edit these 3 parameters
xlsx_filename = 'Google Forms (Responses).xlsx' # input. export to xlsx from Google Drive.
xlsx_sheet_name = 'Form Responses 1' # which tab to extract data from
docx_filename = 'Google Forms (Responses).docx' # output
column_indexes = {}
responses = []
def read_xlsx():
wb = load_workbook(filename = xlsx_filename, use_iterators = True)
ws = wb.get_sheet_by_name(name = xlsx_sheet_name) # ws is now an IterableWorksheet
row_num = 0
for row in ws.iter_rows(): # it brings a new method: iter_rows()
row_num += 1
cell_index = -1
personal_response = {}
for cell in row:
cell_index += 1
if row_num == 1:
try:
column_indexes[cell_index] = cell[3]
except: pass
else:
personal_response[ cell_index ] = cell[3]
responses.append(personal_response)
def write_docx():
document = Document()
for response in responses:
for question_num in response:
document.add_heading(column_indexes[question_num], level=2)
document.add_paragraph(unicode(response[question_num]))
document.add_page_break()
document.save(docx_filename)
if __name__ == '__main__':
read_xlsx()
write_docx()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment