+from PyQt4.QtCore import *
+from PyQt4.QtGui import *
+from PyQt4.QtSql import *
+
+class ODTWriter():
+ def __init__(self, db):
+ self.db = db
+
+ def write(self, filename):
+ doc = QTextDocument()
+ cur = QTextCursor(doc)
+ defaultFormat = QTextCharFormat()
+ defaultFormat.setFontPointSize(12)
+ nameFormat = QTextCharFormat()
+ nameFormat.setFontPointSize(12)
+ nameFormat.setFontWeight(QFont.Bold)
+ q = QSqlQuery(self.db)
+ q.exec_("SELECT * FROM people")
+ rec = q.record()
+ col_id = rec.indexOf("id")
+ col_forename = rec.indexOf("forename")
+ col_surname = rec.indexOf("surname")
+ col_netid = rec.indexOf("netid")
+ col_email = rec.indexOf("email")
+ col_birthday = rec.indexOf("birthday")
+ col_phone = rec.indexOf("phone")
+ col_major = rec.indexOf("major")
+ col_dorm = rec.indexOf("dorm")
+ col_room = rec.indexOf("room")
+ col_photo = rec.indexOf("photo")
+ col_createtime = rec.indexOf("createtime")
+ col_mtime = rec.indexOf("mtime")
+ while q.next():
+ # Write this record to the document
+ id = q.value(col_id).toString()
+ forename = q.value(col_forename).toString()
+ surname = q.value(col_surname).toString()
+ email = q.value(col_email).toString()
+ birthday = q.value(col_birthday).toString()
+ phone = q.value(col_phone).toString()
+ if phone.localeAwareCompare(QString("()==")) == 0: # if they didn't put one in the DB
+ phone = QString("")
+ photo_bin = q.value(col_photo).toByteArray()
+ photo = QImage()
+ photo.loadFromData(photo_bin)
+ # We probably need to normalize photo size here, or set up a QTextFrameFormat
+ if photo.isNull(): # make a black photo for people without one
+ photo = QImage(100,100,QImage.Format_RGB32)
+ photo.fill(0)
+ print "No photo for",forename,surname
+ name = QString("mydata://") + id + QString(".jpg")
+ doc.addResource(QTextDocument.ImageResource, QUrl(name), photo)
+ imageFormat = QTextImageFormat()
+ imageFormat.setName(name)
+ imageFormat.setHeight(100.)
+ imageFormat.setWidth(100.)
+# cur.insertImage(photo)
+ cur.insertText(QString("%1 %2\n").arg(forename).arg(surname), nameFormat )
+ cur.insertText(QString("%1").arg(q.value(col_email).toString()), defaultFormat)
+ cur.insertImage(imageFormat, QTextFrameFormat.FloatLeft)
+ cur.insertText(QString("\n") , defaultFormat)
+ writer = QTextDocumentWriter(filename, QByteArray("odt"))
+ return writer.write(doc)