马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
x
- # -*- 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[1])
- first_week = first.isocalendar()[1]
- last_week = last.isocalendar()[1]
- 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()[1]
- m[week_no % week_month_first, t_weekday] = t_day
-
- print(r'\begin{tabular}{rrrrrrr}')
- print(r'Mo & Di & Mi & Do & Fr & Sa & So \\')
- for i in m:
- if i[1] < 6:
- print('{0} &'.format(m[i]), end='')
- else:
- print('{0}'.format(m[i]),end='')
- if i[1] == 6:
- print(r'\\')
- print(r'\end{tabular}')
-
- gen_cal_latex(2020, 4)
复制代码
|
|