Saturday, February 15, 2014

Create PDF calendars with xtopdf

By Vasudev Ram


As I keep working on xtopdf, my Python toolkit for PDF creation, every now and then I get ideas for new applications that use xtopdf.

Today I thought of using xtopdf with Python to generate PDF calendars.

Here is a program, CalendarToPDF, which shows how to do that:
"""
CalendarToPDF.py
Author: Vasudev Ram - www.dancingbison.com
Copyright 2014 Vasudev Ram
This is a demo program to generate PDF calendars.
"""

import sys
import traceback
from debug1 import debug1
import calendar
from PDFWriter import PDFWriter

try:
    cal = calendar.TextCalendar(calendar.SUNDAY)
    cal_str = cal.formatmonth(2014, 02, 4, 2)
    cal_lines = cal_str.split("\n")
    pw = PDFWriter("Calendar-February-2014.pdf")
    pw.setFont("Courier", 10)
    pw.setHeader("Calendar for February 2014")
    pw.setFooter("Generated by xtopdf: http://bit.ly/xtopdf")
    for line in cal_lines:
        if line != "":
            pw.writeLine(line)
    pw.close()
    print "Calendar generated."
except Exception as e:
    traceback.print_exc()
    sys.exit(1)

This example program generates a simple PDF text calendar for February 2014. Run it with:

python CalendarToPDF.py

This is a screenshot of the resulting PDF output:


Enjoy.

Read posts about xtopdf on my blog.

- Vasudev Ram - Python training and consulting

Contact Page


No comments: