Skip to content

Instantly share code, notes, and snippets.

@fabian-paul
Created December 23, 2020 20:54
Show Gist options
  • Save fabian-paul/44243e78fb6b635dcd728edd48ad166f to your computer and use it in GitHub Desktop.
Save fabian-paul/44243e78fb6b635dcd728edd48ad166f to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import calendar\n",
"import svgwrite\n",
"from svgwrite import cm, mm \n",
"cal = calendar.Calendar()\n",
"year = 2021\n",
"weekday_names = ['-', 'Mo', 'Di', 'Mi', 'Do', 'Fr', 'Sa', 'So']\n",
"month_names = ['-', 'Januar', 'Februar', 'März', 'April', 'Mai', 'Juni', 'Juli', 'August', 'September', 'Oktober', 'November', 'Dezember']\n",
"def monthdays(month):\n",
" for date in cal.itermonthdates(year=year, month=month):\n",
" m = date.month\n",
" d = date.day \n",
" wd = date.isoweekday()\n",
" w = date.isocalendar()[1]\n",
" if m == month:\n",
" yield d, wd, w"
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"291.80000799999976\n"
]
}
],
"source": [
"dayskip = 2.7\n",
"dot = 0.352778\n",
"width = 210/2 - 2 # 210mm / zwei Spalten\n",
"def render(month, svg, x0=0, y0=0, width=210/2):\n",
" 'render month'\n",
" svg.add(svg.text(month_names[month], insert = ((x0 + width/2)*mm, (y0 + 8*dot)*mm), \n",
" font_size=12, fill='black', text_anchor='middle', font_family='serif'))\n",
" y = y0 + 12*dot\n",
" k = 0.0\n",
" for day, weekday, week in monthdays(month):\n",
" weekend = (weekday in [6, 7])\n",
" top = y\n",
" if weekend:\n",
" color = 'red'\n",
" bottom = top + 1.5*dayskip\n",
" else:\n",
" color = 'black'\n",
" bottom = top + dayskip\n",
" middle = 0.5*(top + bottom)\n",
" baseline = middle + 0.8\n",
" if week % 2 == 0:\n",
" svg.add(svg.rect(insert = (x0*mm, top*mm), \n",
" size = ((x0 + width)*mm, (bottom-top)*mm), \n",
" fill = \"rgb(240,240,240)\"))\n",
" \n",
" svg.add(svg.text(weekday_names[weekday], \n",
" insert = ((x0 + 4)*mm, baseline*mm), font_size=10, fill=color, font_family='serif'))\n",
" svg.add(svg.text(str(day), \n",
" insert = (x0*mm, baseline*mm), font_size=10, fill=color, font_family='serif'))\n",
" svg.add(svg.line(\n",
" start=(x0*mm, top*mm), \n",
" end=((x0 + width)*mm, top*mm), stroke='black', stroke_width=0.3))\n",
" y = bottom \n",
" svg.add(svg.line(\n",
" start=(x0*mm, bottom*mm), \n",
" end=((x0 + width)*mm, bottom*mm), stroke='black', stroke_width=0.3))\n",
" return bottom\n",
"\n",
"svg_document = svgwrite.Drawing(filename = \"Kalender-2021-S1.svg\", size = (\"210mm\", \"297mm\"))\n",
"y = render(1, svg_document, y0=1.)\n",
"y = render(2, svg_document, y0=y)\n",
"y = render(3, svg_document, y0=y)\n",
"print(y)\n",
"y = render(4, svg_document, y0=1., x0=width+4)\n",
"y = render(5, svg_document, y0=y, x0=width+4)\n",
"y = render(6, svg_document, y0=y, x0=width+4)\n",
"svg_document.save()"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"297.2000079999998\n"
]
}
],
"source": [
"svg_document = svgwrite.Drawing(filename = \"Kalender-2021-S2.svg\", size = (\"210mm\", \"297mm\"))\n",
"y = render(7, svg_document, y0=1.)\n",
"y = render(8, svg_document, y0=y)\n",
"y = render(9, svg_document, y0=y)\n",
"print(y)\n",
"y = render(10, svg_document, y0=1., x0=width+4)\n",
"y = render(11, svg_document, y0=y, x0=width+4)\n",
"y = render(12, svg_document, y0=y, x0=width+4)\n",
"svg_document.save()"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"!/Applications/Inkscape\\ 2.app/Contents/MacOS/inkscape --export-type=pdf --export-filename=/Users/fab/Pictures/Grafik/Kalender-2019/Kalender-2021-S2.pdf \"/Users/fab/Pictures/Grafik/Kalender-2019/Kalender-2021-S2.svg\""
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"!/Applications/Inkscape\\ 2.app/Contents/MacOS/inkscape --export-type=pdf --export-filename=/Users/fab/Pictures/Grafik/Kalender-2019/Kalender-2021-S1.pdf \"/Users/fab/Pictures/Grafik/Kalender-2019/Kalender-2021-S1.svg\""
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.4"
},
"toc": {
"base_numbering": 1,
"nav_menu": {},
"number_sections": true,
"sideBar": true,
"skip_h1_title": false,
"title_cell": "Table of Contents",
"title_sidebar": "Contents",
"toc_cell": false,
"toc_position": {},
"toc_section_display": true,
"toc_window_display": false
}
},
"nbformat": 4,
"nbformat_minor": 2
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment