Created
February 22, 2018 07:50
-
-
Save carrotsword/001e5150fb1aa9bfa076527b6511e95d to your computer and use it in GitHub Desktop.
generate svg calendar image
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import calendar | |
from datetime import date | |
CEL_WIDTH = 28 | |
CEL_HEIGHT = 28 | |
CEL_TEXT_ADJUST_X = CEL_WIDTH / 2.0 -1 | |
CEL_TEXT_ADJUST_Y = 20 | |
WIDTH = CEL_WIDTH * 7 + 2 | |
HEIGHT = CEL_HEIGHT * 6 + 2 | |
today = date.today() | |
target = date(today.year, today.month, 1) | |
monthrange = calendar.monthrange(today.year, today.month) | |
dom = 1 | |
day = monthrange[0] | |
week = 0 | |
f = open("/tmp/calendar.svg", "w") | |
f.write("<svg xmlns=\"http://www.w3.org/2000/svg\" id=\"erd-canvas\" width=\"%dpx\" height=\"%dpx\" >" % (WIDTH, HEIGHT)) | |
for day_num in range(1, monthrange[1]): | |
target = date(target.year, target.month, day_num) | |
fill = "#fff" | |
char_stroke = "#000" | |
if target.weekday() == 6: | |
week += 1 | |
fill = "#fee" | |
char_stroke = "#f00" | |
elif target.weekday() == 5: | |
fill = "#eef" | |
char_stroke = "#00f" | |
if today.day == day_num: | |
fill = "#fc8" | |
x = ((target.weekday() + 1) % 7) * CEL_WIDTH | |
y = week * CEL_HEIGHT | |
f.write("<rect x=\"%dpx\" y=\"%dpx\" width=\"%dpx\" height=\"%dpx\" " | |
" stroke=\"#000\" stroke-width=\"1px\" fill=\"%s\"/>" | |
% (x + 1, y + 1, CEL_WIDTH - 2, CEL_HEIGHT - 2, fill)) | |
f.write("<text x=\"%dpx\" y=\"%dpx\" stroke=\"%s\" text-anchor=\"middle\">%d</text>" | |
% (x + CEL_TEXT_ADJUST_X, y + CEL_TEXT_ADJUST_Y, char_stroke, day_num)) | |
f.write("</svg>") | |
f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment