#!/usr/bin/env python # -*- coding: iso-8859-1 -*- # This script is free software. # (c) 2004 Thomas Guettler # Python Imports import os import re import sys import time import getopt def usage(): print """Usage: %s [-d description.txt] YEAR out.tex Create a latex file for the year YEAR. If you want two pages on one run this after pdflatex: pdftops out.pdf; pstops '2:0L@.7(21cm,0)+1L@.7(21cm,14.85cm)' out.ps out2.ps ; ps2pdf out2.ps; pdftops out2.pdf; lpr out2.ps The last two steps look unnecassary, but I got ugly fonts without this. -d description.txt This text file contains lines which start with the number of the month, following some text for this page. Example: 1 Nice weather in January 2 Top: Me\\newline Bottom: You ... """ % ( os.path.basename(sys.argv[0])) days_dict={ 0: "Mo", 1: "Di", 2: "Mi", 3: "Do", 4: "Fr", 5: "Sa", 6: "So"} months_dict={ 1: "Januar", 2: "Februar", 3: "März", 4: "April", 5: "Mai", 6: "Juni", 7: "Juli", 8: "August", 9: "September", 10: "Oktober", 11: "November", 12: "Dezember"} def main(): try: opts, args = getopt.getopt(sys.argv[1:], "d:", []) except getopt.GetoptError, e: print e usage() sys.exit(3) descriptionfile=None for o, a in opts: if o=="-d": descriptionfile=a if not os.path.isfile(descriptionfile): print "%s does not exist" % descriptionfile sys.exit(3) if len(args)!=2: usage() sys.exit() year=args[0] outfile=args[1] try: year=int(year) except ValueError: usage() sys.exit(3) descriptions={} if descriptionfile: fd=open(descriptionfile) old=0 for line in fd: line=line.strip() if not line or line.startswith("#"): continue match=re.match(r'^(\d+)\s+(.*)$', line) if not match: print "SyntaxError in %s line: %s" % ( description, line) sys.exit(3) month=int(match.group(1)) assert month<=12 and month>=1 if month<=old: print "SyntaxError old=%d new month=%d" % ( old, month) sys.exit(3) descriptions[month]=match.group(2) old=month t=[year, 1, 1, 0, 0, 0, 0, 0, -1] year_start=year month_old=0 week=[] months_list=[] while 1: t=list(time.localtime(time.mktime(t))) year=t[0] month=t[1] day=t[2] weekday=t[6] if month!=month_old: if month_old!=0: # Fill empty cells at end of old month if weekday!=0: for i in range(7-weekday): week.append("") weeks.append(week) months_list.append(weeks) weeks=[] week=[] for i in range(weekday): week.append("") else: if weekday==0: if not len(week)==7: raise("week has not 7 elements: %s" % week) weeks.append(week) week=[] if year!=year_start: break print "%s %s %s" % (day, months_dict[month], days_dict[weekday]) week.append(day) t[2]+=1 month_old=month assert(len(months_list)==12) months_tex=[] weekdays_tex=[] for i in range(7): weekdays_tex.append(days_dict[i]) weekdays_tex=' & '.join(weekdays_tex) for i in range(0, 12): weeks=months_list[i] weeks_tex=[] assert(weeks) for week in weeks: if not len(week)==7: raise("Not 7 elements in week: %s" % week) week_tex=[] for day in week: week_tex.append(str(day)) week_tex=' & '.join(week_tex) weeks_tex.append(week_tex) if day<27: raise("Error: %s %s" % (week, weeks)) weeks_tex=' \\\\\n'.join(weeks_tex) text=descriptions.get(i+1, "") print "t", text months_tex.append( r""" \vspace*{18.4cm} \begin{center} {\large %s } \end{center} %s \begin{tabular}{|r|r|r|r|r|r|r|} %s \\ %s \end{tabular} %s """ % ( text, months_dict[i+1], weekdays_tex, weeks_tex, year_start)) months_tex='\n\\newpage\n'.join(months_tex) tex=r"""\documentclass[a4paper,11pt]{report} \usepackage{ngerman} \usepackage[latin1]{inputenc} \usepackage[T1]{fontenc} \usepackage{vmargin} \setpapersize[portrait]{A4} \setmarginsrb{1in}{1in}{1in}{0.5cm}{0in}{0in}{0in}{0in} \pagestyle{empty} \begin{document} \noindent \flushbottom \raggedbottom \huge \bfseries %s \end{document} """ % months_tex fd=open(outfile, "wt") fd.write(tex) fd.close() print "Created %s" % outfile if __name__=="__main__": main()