Tuesday, November 18, 2008

Meetings and Boredom continue

I am sitting on yet another boring meeting and have written a script to write it to a PDF. Its not a pretty one but I got it done in 30 mins. That has to count for something. You would need ReportLab and PIL for this.

import re
from reportlab.platypus import Image, SimpleDocTemplate, Spacer,Flowable
from reportlab.lib.units import inch
import dircache
import Image as PILImage
import os.path
months = ["January", "Feburary", "March","April","May","June","July","August","September","October","November","December"]
class Bookmark(Flowable):
def __init__(self,title,key,level):
self.title=title
self.key=key
self.level=level
Flowable.__init__(self)
def wrap(self, availWidth, availHeight):
return (0,0)
def draw(self):
self.canv.showOutline()
self.canv.bookmarkPage(self.key)
self.canv.addOutlineEntry(self.title,self.key,self.level, 0)
base_dir='c:\\work\\toons\\dilbert\\'
files=filter(lambda x: x.endswith('gif'),dircache.listdir(base_dir))
doc=SimpleDocTemplate('dilbert.pdf')
oldyear=0
oldmonth=0
contents=[]
contents.append(Bookmark("Contents","Contents",0))
for f in files:
m=re.match(r'(\d\d\d\d)\-(\d\d)\-(\d\d)\.gif',f)
if(m==None):
continue
m=m.groups()
if(oldyear!=int(m[0]) or oldmonth!=int(m[1])):
contents.append( Bookmark("" + months[int(m[1])-1] + " " + str(int(m[0])),"" + months[int(m[1])-1] + " " + str(int(m[0])),1))
oldyear=int(m[0])
oldmonth=int(m[1])
#print os.path.join(base_dir,f)
fname=os.path.join(base_dir,f)
size=PILImage.open(fname).size
#print size
contents.append(Spacer(1,inch))
contents.append(Image(fname,doc.width,size[1]*(doc.width/size[0])))
print 'building...'
doc.build(contents)


Beautiful Soup and ReportLab rock.

No comments: