How to use python to create xlsx file for kahoot quiz

With this script you will be faster making kahoot quizzes

import openpyxl as opx
import os
from random import shuffle


wb = opx.load_workbook("esempio.xlsx")
ws = wb.active


data1 = """
Question 1
1
2
3
4
30

Question 2
10
20
30
40
60"""

data2 = data1[1:].split("\n\n")

data3 = []
for d in data2: # ['\nQuestion\n1\n2\n3\n4\n30', 'Question\n1\n2\n3\n4\n30\n']
	data3.append(d.split("\n"))
print(data3)

data4 = []
for d in data3:
	# [['Question', '1', '2', '3', '4', '30'], ['Question', '10', '20', '30', '40', '60']]
	if len(d) == 6:
		data4.append([d[0], ",".join([d[1],d[2],d[3],d[4]]), d[5]])
	if len(d) == 5:
		data4.append([d[0], ",".join([d[1],d[2],d[3]]), d[4]])
	if len(d) == 4:
		data4.append([d[0], ",".join([d[1],d[2]]), d[3]])

print(data4)

count = 9
for d in data4:
	print(count)
	correct = d[1].split(",")[0] 
	rnd = d[1].split(",")
	lenr = len(rnd)
	shuffle(rnd)
	print(rnd)
	correct = rnd.index(correct) + 1
	match lenr:
		case 4:
			r1, r2, r3, r4 = rnd

			ws[f"C{count}"] = r1
			ws[f"D{count}"] = r2
			ws[f"E{count}"] = r3
			ws[f"F{count}"] = r4
		case 3:
			r1, r2, r3 = rnd
			ws[f"C{count}"] = r1
			ws[f"D{count}"] = r2
			ws[f"E{count}"] = r3
		case 2:
			r1, r2 = rnd
			ws[f"C{count}"] = r1
			ws[f"D{count}"] = r2

	ws[f"B{count}"] = d[0]
	ws[f"G{count}"] = d[2]
	ws[f"H{count}"] = correct
	count += 1

wb.save("esempio.xlsx")
os.startfile("esempio.xlsx")

Subscribe to the newsletter for updates
Tkinter templates
Avatar My youtube channel

Twitter: @pythonprogrammi - python_pygame

Videos

Speech recognition game

Pygame's Platform Game

Other Pygame's posts

Published by pythonprogramming

Started with basic on the spectrum, loved javascript in the 90ies and python in the 2000, now I am back with python, still making some javascript stuff when needed.