Kochbuch/kekse.py

313 lines
12 KiB
Python
Raw Normal View History

2021-06-05 20:48:10 +00:00
#!/usr/bin/env python3
import os
2023-02-20 13:16:23 +00:00
import re
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
from openpyxl import load_workbook, Workbook
from openpyxl.worksheet.worksheet import Worksheet
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
def escape_latex(content):
content = content.replace("", "/")
# content = content.replace("℔", r"\ lb")
content = content.replace("½", "$\\frac{1}{2}$")
content = content.replace("¼", "$\\frac{1}{4}$")
content = content.replace("¾", "$\\frac{3}{4}$")
content = content.replace("\n", "\n\n")
content = content.replace("°", r"$^\circ$")
content = content.replace("&", r"\&")
2021-06-05 20:48:10 +00:00
content = content.replace("", "ö")
2023-02-20 13:16:23 +00:00
content = re.sub(r'(\d) (\d+)/(\d+)', r'$\1\\frac{\2}{\3}$', content)
content = re.sub(r'(\d+)/(\d+)', r'$\\frac{\1}{\2}$', content)
content = re.sub(r'([\d$]g?) ?- ?([\d$])', '\\1--\\2', content)
content = content.replace("$ ", r"$\,")
2021-06-05 20:48:10 +00:00
return content
# Define output file
2023-02-20 13:16:23 +00:00
f_latex = open("rezepte.tex", "w+")
f_latex.write("%!TEX ROOT = ./main.tex\n\n")
2021-06-05 20:48:10 +00:00
# Load cookbook
2023-02-20 13:16:23 +00:00
cb: Workbook = load_workbook('kekse.xlsx', data_only=True)
2021-06-05 20:48:10 +00:00
sheets = cb.sheetnames
# Find worksheet with recipes
2023-02-20 13:16:23 +00:00
ws: Worksheet = cb["Rezepte"]
2021-06-05 20:48:10 +00:00
# Variables to store information while parsing
# Legend gives the column with descriptions and starting row for the recipes
legend_row = 0
legend_col = 0
# Ingredients
# Find the corresponding rows to all properties of recipes
ingredients_row_start = 0
in_ingredients = False
in_recipe = False
ingredients = {}
ingredients_unit = {}
ingredients_comment = {}
2023-02-20 13:16:23 +00:00
ingredients_other_row = 0
2021-06-05 20:48:10 +00:00
# Other fields
type_row = 0
instructions_row = 0
baking_time_row = 0
baking_temperature_row = 0
number_row = 0
resting_time_row = 0
specials_row = 0
# Years
first_year_row = 0
last_year_row = 0
first_year = 0
#####
# Go through worksheet and parse all information
for column in ws.columns:
for cell in column:
# Find row of type of recipe
2023-02-20 13:16:23 +00:00
if cell.value == "Art":
2021-06-05 20:48:10 +00:00
type_row = cell.row
legend_row = cell.row
legend_col = cell.column
2023-02-20 13:16:23 +00:00
if cell.column == legend_col:
2021-06-05 20:48:10 +00:00
# Find row of recipe name, this is in the row with 'Zutaten'
2023-02-20 13:16:23 +00:00
if cell.value == "Zutaten":
2021-06-05 20:48:10 +00:00
name_row = cell.row
2023-02-20 13:16:23 +00:00
ingredients_row_start = cell.row + 1
2021-06-05 20:48:10 +00:00
in_ingredients = True
# Find position of ingredients
2023-02-20 13:16:23 +00:00
if cell.value == "Hauptaromen":
2021-06-05 20:48:10 +00:00
aroma_row = cell.row
# Find position of source
2023-02-20 13:16:23 +00:00
if cell.value == "Quelle":
2021-06-05 20:48:10 +00:00
source_row = cell.row
# Instructions
2023-02-20 13:16:23 +00:00
if cell.value == "Anleitung":
2021-06-05 20:48:10 +00:00
instructions_row = cell.row
# Check if we are at end of ingredients
2023-02-20 13:16:23 +00:00
if cell.value == "Sonstiges (Deko)":
2021-06-05 20:48:10 +00:00
ingredients_other_row = cell.row
in_ingredients = False
# Make list of all ingredients and their units
2023-02-20 13:16:23 +00:00
if cell.row >= ingredients_row_start and in_ingredients:
2021-06-05 20:48:10 +00:00
string = cell.value
2023-02-20 13:16:23 +00:00
if "(" in string and ")" in string:
m = re.match(r'^([^(\n]*)(?: \((\S+ [^)]+)\))?(?: \(([^)]+)\))?$', cell.value)
assert m
ingredients[cell.row] = m.group(1)
ingredients_unit[cell.row] = m.group(3)
ingredients_comment[cell.row] = m.group(2)
2021-06-05 20:48:10 +00:00
else:
2023-02-20 13:16:23 +00:00
ingredients[cell.row] = string
ingredients_unit[cell.row] = ""
ingredients_comment[cell.row] = ""
2021-06-05 20:48:10 +00:00
# Further instructions
2023-02-20 13:16:23 +00:00
if cell.value == "Backzeit (Minuten)":
2021-06-05 20:48:10 +00:00
baking_time_row = cell.row
2023-02-20 13:16:23 +00:00
if cell.value == "Backtemperatur Umluft (Grad)":
2021-06-05 20:48:10 +00:00
baking_temperature_row = cell.row
2023-02-20 13:16:23 +00:00
if cell.value == "Ruhezeit (Minuten)":
2021-06-05 20:48:10 +00:00
resting_time_row = cell.row
2023-02-20 13:16:23 +00:00
if cell.value == "Anzahl (ca.)":
2021-06-05 20:48:10 +00:00
number_row = cell.row
2023-02-20 13:16:23 +00:00
if cell.value == "Besonderheiten":
2021-06-05 20:48:10 +00:00
specials_row = cell.row
# Years
2023-02-20 13:16:23 +00:00
if cell.value == 2013:
2021-06-05 20:48:10 +00:00
first_year_row = cell.row
last_year_row = cell.row
first_year = 2013
2023-02-20 13:16:23 +00:00
if first_year_row > 0 and cell.value == "Kommentare zur Bearbeitung.":
2021-06-05 20:48:10 +00:00
# cell.row -2 instead of -1 to ignore current year 2021
2023-02-20 13:16:23 +00:00
last_year_row = cell.row - 2
2021-06-05 20:48:10 +00:00
# By now, we should have found everything in the legend
2023-02-20 13:16:23 +00:00
2021-06-05 20:48:10 +00:00
#####
# Parse recipe
# Go through worksheet vertically, starting with the first column
# If right of legend, then it must be a recipe
2023-02-20 13:16:23 +00:00
if cell.column > legend_col:
2021-06-05 20:48:10 +00:00
# Obtain information of recipe in the right order, so that we can directly write it to the tex-file.
# Name of recipe
2023-02-20 13:16:23 +00:00
content = ws.cell(name_row, cell.column).value
if content != "None\n" and content is not None:
f_latex.write("\\newpage\n")
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
# if (content[-5:]=="(Oma)"):
2021-06-05 20:48:10 +00:00
# content="Omas " + content[:-6]
2023-02-20 13:16:23 +00:00
f_latex.write("\\section{%s}\\vspace{0.5cm minus 0.5cm}\n" % content)
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
f_latex.write("\\begin{tabular}{lll}\n")
2021-06-05 20:48:10 +00:00
# Aroma
2023-02-20 13:16:23 +00:00
content = ws.cell(aroma_row, cell.column).value
if content != "None\n" and content is not None:
f_latex.write("\\faHeart & \\textbf{Hauptzutat} & %s" % content)
f_latex.write("\\index{\\textbf{%s}}" % content.replace(", ", "}} \\index{\\textbf{"))
2021-06-05 20:48:10 +00:00
f_latex.write("\\\\\n")
# Type
2023-02-20 13:16:23 +00:00
content = ws.cell(type_row, cell.column).value
if content != "None\n" and content is not None:
f_latex.write("\\faEye & \\textbf{Form} & %s" % content)
f_latex.write("\\index[formen]{\\textbf{%s}}" % content.replace(", ", "}} \\index[formen]{\\textbf{"))
2021-06-05 20:48:10 +00:00
f_latex.write("\\\\\n")
# Source
2023-02-20 13:16:23 +00:00
content = ws.cell(source_row, cell.column).value
if content != "None\n" and content is not None:
f_latex.write("\\faBook & \\textbf{Quelle} & %s" % content)
f_latex.write("\\\\\n")
2021-06-05 20:48:10 +00:00
# Specials
2023-02-20 13:16:23 +00:00
content = ws.cell(specials_row, cell.column).value
2021-11-07 21:46:26 +00:00
if content == "(vegan)" or content == "(Vegan)":
2023-02-20 13:16:23 +00:00
content = "Vegan möglich"
if content == "(zuckerfrei)" or content == "(Zuckerfrei)":
2023-02-20 13:16:23 +00:00
content = "Zuckerfrei möglich"
if content != "None\n" and content is not None:
f_latex.write("\\faStar & \\textbf{Besonderheiten} & %s" % content)
f_latex.write("\\index[specials]{\\textbf{%s}}" % content.replace(", ", "}} \\index[specials]{\\textbf{"))
f_latex.write("\\\\\n")
2021-06-05 20:48:10 +00:00
# Baking
2023-02-20 13:16:23 +00:00
content1 = str(ws.cell(baking_time_row, cell.column).value)
content2 = str(ws.cell(baking_temperature_row, cell.column).value)
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
if (content1 != "None" and content1 is not None and content1 != "0") or (content2 != "None" and content2 is not None and content2 != "0"):
f_latex.write("\\Oven & \\textbf{Backzeit} &")
if content1 != "None" and content1 is not None and content1 != "0":
if content1[-4:] == "Min.":
2021-06-05 20:48:10 +00:00
content1 = content1[:-4]
2023-02-20 13:16:23 +00:00
content1 = re.sub(r'(\d) ?- ?(\d)', '\\1--\\2', content1)
f_latex.write("%s Minuten bei " % content1)
2021-06-05 20:48:10 +00:00
else:
2023-02-20 13:16:23 +00:00
f_latex.write(" bei")
if content2 != "None" and content2 is not None and content2 != "0":
f_latex.write(" %s~$^\circ$C" % content2)
2021-06-05 20:48:10 +00:00
f_latex.write("\\\\\n")
# Resting time
2023-02-20 13:16:23 +00:00
content = str(ws.cell(resting_time_row, cell.column).value)
if content != "None" and content is not None and content != "0":
f_latex.write("\\faClockO & \\textbf{Ruhezeit} &")
if content[-1:] == "h":
f_latex.write(" %s Stunden" % content[:-1])
2021-06-05 20:48:10 +00:00
else:
2023-02-20 13:16:23 +00:00
f_latex.write("%s Minuten" % content)
2021-06-05 20:48:10 +00:00
f_latex.write("\\\\\n")
# Amount
2023-02-20 13:16:23 +00:00
content = ws.cell(number_row, cell.column).value
if content != "None\n" and content is not None:
f_latex.write("\\faHashtag & \\textbf{Anzahl} & %s Stück\\\\\n" % content)
2021-06-05 20:48:10 +00:00
# Years
year = first_year
count = 0
output = "\\faCalendar & \\textbf{Gebacken} &"
2023-02-20 13:16:23 +00:00
for i in range(first_year_row, last_year_row + 1):
content = ws.cell(i, cell.column).value
if content != "None\n" and content is not None:
2021-06-05 20:48:10 +00:00
# Cookie was baked in year
output = output + " " + str(year) + ","
2023-02-20 13:16:23 +00:00
count += 1
2021-06-05 20:48:10 +00:00
year += 1
2023-02-20 13:16:23 +00:00
if count == 0:
2021-06-05 20:48:10 +00:00
output = output + " Noch nie gebacken"
else:
output = output[:-1]
2023-02-20 13:16:23 +00:00
f_latex.write(str(output + "\\\\\n"))
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
if count > 0:
f_latex.write("\\faLineChart & \\textbf{Häufigkeit} & " + str(round(100 / (year - first_year) * count)) + "\\,\\%\\\\\n")
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
f_latex.write("\\end{tabular}\n")
2021-06-05 20:48:10 +00:00
# List of Ingredients
2023-02-20 13:16:23 +00:00
f_latex.write("\\subsection*{Zutaten}\n")
f_latex.write("\\begin{multicols}2\\raggedright\n")
f_latex.write("\\begin{compactitem}\n")
for i in range(ingredients_row_start, ingredients_other_row):
number = str(ws.cell(i, cell.column).value)
if number != "None\n" and number is not None and number != "None":
# Add unit and whitespaces
if not ingredients_unit[i]:
unit = ""
elif ingredients_unit[i][0].islower():
unit = "\\," + ingredients_unit[i]
else:
unit = "~" + ingredients_unit[i]
2021-06-05 20:48:10 +00:00
# Add Ingredient name
2023-02-20 13:16:23 +00:00
label = ingredients[i]
if ingredients[i] == "Ei":
unit = ""
if number == "1":
label = "Ei"
2021-06-05 20:48:10 +00:00
else:
2023-02-20 13:16:23 +00:00
label = "Eier"
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
# Add comment if available
if ingredients_comment[i]:
label += r" \penalty-10(" + ingredients_comment[i] + ")"
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
label = escape_latex(label)
f_latex.write("\\item %s%s %s \n" % (number, unit, label))
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
f_latex.write("\\end{compactitem}\n")
f_latex.write("\\end{multicols}\n")
f_latex.write("\\vspace{0.2cm minus 0.2cm}\n")
2021-06-05 20:48:10 +00:00
# Other ingredients
2023-02-20 13:16:23 +00:00
content = ws.cell(ingredients_other_row, cell.column).value
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
if content != "None\n" and content is not None and content != "None":
content = escape_latex(content)
f_latex.write("\\textit{Außerdem:} %s \n" % content)
2021-06-05 20:48:10 +00:00
# Instructions
2023-02-20 13:16:23 +00:00
content = ws.cell(instructions_row, cell.column).value
if content != "None\n" and content is not None:
content = escape_latex(content)
content = re.sub(r'/([a-z])', r'/\\allowbreak{}\1', content, flags=re.I)
content = re.sub(r'(\d) ([a-z])', r'\1~\2', content, flags=re.I)
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
f_latex.write("\\subsection*{Anleitung} %s\n" % content)
2021-06-05 20:48:10 +00:00
2023-02-20 13:16:23 +00:00
f_latex.write("\\vspace{2cm minus 1cm}\n")
2021-06-05 20:48:10 +00:00
# That's all, so close files
f_latex.close()
os.system("latexmk -pdf main.tex")