德国大神开发的LaTeX日历生成工具

电脑技术 电脑技术 3715 人阅读 | 0 人回复 | 2022-04-15

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. # -*- coding: utf-8 -*-

  2. import calendar
  3. import datetime

  4. def number_of_weeks(year, month):
  5.     """
  6.         Returns a tupel with the ISO no of the first and week and the no of weeks
  7.     """
  8.     tup_month_days = calendar.monthrange(year, month)
  9.     first = datetime.date(year, month, 1)
  10.     last = datetime.date(year, month, tup_month_days[1])
  11.     first_week = first.isocalendar()[1]
  12.     last_week = last.isocalendar()[1]
  13.     return (first_week, last_week, last_week-first_week+1)

  14. def gen_cal_latex(year, month):
  15.     """
  16.         https://stackoverflow.com/questions/9459337/assign-value-to-an-individual-cell-in-a-two-dimensional-python-array
  17.     """
  18.     c = calendar.TextCalendar()
  19.     week_month_first, week_month_last, no_of_weeks = number_of_weeks(year, month)

  20.     # generate calendar list, using tupel as a key   
  21.     m = {(i, j):' ' for i in range(no_of_weeks) for j in range(7)}

  22.     for tupel_date in c.itermonthdays4(year, month):
  23.         t_year, t_month, t_day, t_weekday = tupel_date
  24.         # use only dates inside the required month
  25.         if t_month == month:
  26.             temp_date = datetime.date(t_year, t_month, t_day)
  27.             # check in which week we are with the current date
  28.             # to get index for the list
  29.             week_no = temp_date.isocalendar()[1]
  30.             m[week_no % week_month_first, t_weekday] = t_day

  31.     print(r'\begin{tabular}{rrrrrrr}')
  32.     print(r'Mo & Di & Mi & Do & Fr & Sa & So \\')
  33.     for i in m:
  34.         if i[1] < 6:
  35.             print('{0} &'.format(m[i]), end='')
  36.         else:
  37.             print('{0}'.format(m[i]),end='')
  38.         if i[1] == 6:
  39.             print(r'\\')
  40.     print(r'\end{tabular}')

  41. gen_cal_latex(2020, 4)
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

热门推荐