csv-to-marp-converter/converter.py
2021-06-15 16:14:04 +02:00

84 lines
2.9 KiB
Python

import calendar
import os
import shutil
# def file_exists(filename):
# if os.path.exists(filename):
# print('Note: Diary \'' + filename + '\' already exists. Will not be modified.')
# return True
# return False
NUMBER_OF_ANSWERS = 5
class QuestionAndAnswers:
def __init__(self, question, answerArray, category):
self.question = question
self.answerArray = answerArray
self.category = category
def __str__(self):
return "QuestionAndAnswers: Question: {}, Answers: {}, Category: {}".format(self.question, self.answerArray, self.category)
def write_markdown_file(csv_path):
# csv_path = "quiz-example.csv"
markdown_path = "slide-deck.md"
# read CSV
list_of_q_and_as = []
with open(csv_path, 'r', encoding='UTF-8') as csv_file:
data = csv_file.readlines()
current_category = "bla"
for line in data:
if line.startswith("Kategorie"):
name_of_category = line.split(",")[0]
print("name_of_category")
print(name_of_category)
current_category = name_of_category
else:
handle_q_and_a_row(line, list_of_q_and_as, current_category)
# break
# static_part = ""
# copy template
template_name = "quiz-slides-template.md"
shutil.copyfile(template_name, markdown_path)
# with open(markdown_path, 'w', encoding='UTF-8') as markdown_file:
# append questions and answers to copy of template
with open(markdown_path, 'a', encoding='UTF-8') as markdown_file:
# write month headline
# markdown_file.writelines(static_part)
for q_and_a in list_of_q_and_as:
print(q_and_a)
markdown_file.writelines('\n')
markdown_file.writelines('# {}'.format(q_and_a.question))
markdown_file.writelines('\n')
markdown_file.writelines(' - {}'.format(q_and_a.answerArray[0]))
markdown_file.writelines('\n')
markdown_file.writelines(' - {}'.format(q_and_a.answerArray[1]))
markdown_file.writelines('\n')
markdown_file.writelines(' - {}'.format(q_and_a.answerArray[2]))
markdown_file.writelines('\n')
markdown_file.writelines(' - {}'.format(q_and_a.answerArray[3]))
markdown_file.writelines('\n')
markdown_file.writelines('---')
markdown_file.close()
# return filename
def handle_q_and_a_row(line, list_of_q_and_as, category):
column_data = line.split(',')
print(column_data)
# check size of array
print(len(column_data))
if len(column_data) is NUMBER_OF_ANSWERS:
question = column_data[0]
answers = column_data[1:] # select all elements from list except first
# answers = column_data[-3:]
q_and_a = QuestionAndAnswers(question, answers, category)
list_of_q_and_as.append(q_and_a)
print(q_and_a)
return list_of_q_and_as