德国大神开发的LaTeX日历生成工具
# -*- coding: utf-8 -*-import calendar
import datetime
def number_of_weeks(year, month):
"""
Returns a tupel with the ISO no of the first and week and the no of weeks
"""
tup_month_days = calendar.monthrange(year, month)
first = datetime.date(year, month, 1)
last = datetime.date(year, month, tup_month_days)
first_week = first.isocalendar()
last_week = last.isocalendar()
return (first_week, last_week, last_week-first_week+1)
def gen_cal_latex(year, month):
"""
https://stackoverflow.com/questions/9459337/assign-value-to-an-individual-cell-in-a-two-dimensional-python-array
"""
c = calendar.TextCalendar()
week_month_first, week_month_last, no_of_weeks = number_of_weeks(year, month)
# generate calendar list, using tupel as a key
m = {(i, j):' ' for i in range(no_of_weeks) for j in range(7)}
for tupel_date in c.itermonthdays4(year, month):
t_year, t_month, t_day, t_weekday = tupel_date
# use only dates inside the required month
if t_month == month:
temp_date = datetime.date(t_year, t_month, t_day)
# check in which week we are with the current date
# to get index for the list
week_no = temp_date.isocalendar()
m = t_day
print(r'\begin{tabular}{rrrrrrr}')
print(r'Mo & Di & Mi & Do & Fr & Sa & So \\')
for i in m:
if i < 6:
print('{0} &'.format(m), end='')
else:
print('{0}'.format(m),end='')
if i == 6:
print(r'\\')
print(r'\end{tabular}')
gen_cal_latex(2020, 4)
页:
[1]