csv-to-marp-converter/converter.py
2021-06-16 16:55:11 +02:00

123 lines
4.0 KiB
Python

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
BASE_FODLER = "test_data"
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 convert(csv_path, output_pdf=False):
write_markdown_file(csv_path)
if (output_pdf):
to_pdf("slide-deck.md")
def write_markdown_file(csv_path):
# csv_path = "quiz-example.csv"
OUTPUT_FODLER = "output"
markdown_filename = "slide-deck.md"
markdown_path = os.path.join(OUTPUT_FODLER, markdown_filename)
# read CSV
list_of_q_and_as = []
with open(csv_path, 'r', encoding='UTF-8') as csv_file:
data = csv_file.readlines()
current_category = ""
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
# do nothing if row is empty
elif line.split(",")[0] is "":
pass
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)
write_q_and_a_to_markdown_file(markdown_file, q_and_a)
markdown_file.close()
# return filename
def write_q_and_a_to_markdown_file(markdown_file, q_and_a):
markdown_file.writelines('\n')
markdown_file.writelines('# {}'.format(q_and_a.category))
markdown_file.writelines('\n')
markdown_file.writelines('## {}'.format(q_and_a.question))
import string
alphabet = string.ascii_lowercase
# for i, obj in q_and_a.answerArray:
for i, obj in enumerate(q_and_a.answerArray):
markdown_file.writelines('\n')
alpha = alphabet[i]
markdown_file.writelines(' - {}) {}'.format(alpha, q_and_a.answerArray[i]))
markdown_file.writelines('---')
return markdown_file
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
# docker run --rm --init -v $PWD:/home/marp/app/ -e LANG=$LANG marpteam/marp-cli slide-deck.md --pdf
# minimal (windows): docker run -v ${PWD}:/home/marp/app/ marpteam/marp-cli slide-deck.md --pdf
def to_pdf(filename):
import docker
client = docker.from_env()
current_dir = os.getcwd()
print(current_dir)
OUTPUT_FODLER = "output"
output_folder_path = os.path.join(current_dir, OUTPUT_FODLER)
# https://docker-py.readthedocs.io/en/stable/containers.html
client.containers.run('marpteam/marp-cli', '{} --pdf'.format(filename), volumes=[output_folder_path + ':/home/marp/app/'],
name="marp", detach=True, remove=True, init=True)
container = client.containers.get('marp')
print(container.logs())