From: Drew Fisher Date: Sat, 22 Aug 2009 03:12:54 +0000 (-0500) Subject: Export to ODT. X-Git-Url: https://git.zarvox.org/shortlog/month?a=commitdiff_plain;h=9d5cd0d7731852cea2d3d5575845a36cb0e6e5b1;p=wp3.git Export to ODT. A limited ODT exporter. It's not pretty, nor suitable for final printing, but illustrates the concept and mechanism. --- diff --git a/mainapp.py b/mainapp.py index e57fe55..1c96c10 100644 --- a/mainapp.py +++ b/mainapp.py @@ -5,6 +5,7 @@ from PyQt4.QtSql import * from chooseaction import ChooseAction from editperson import EditPerson from mergephotos import MergePhotos +from odtwriter import ODTWriter class MainApp (QMainWindow): schema = "CREATE TABLE people ( id INTEGER PRIMARY KEY AUTOINCREMENT, forename TEXT NOT NULL, surname TEXT NOT NULL, netid TEXT NOT NULL, email TEXT, birthday TEXT, phone TEXT, major TEXT, dorm TEXT, room INTEGER, photo BLOB, createtime TEXT NOT NULL, mtime TEXT NOT NULL);" @@ -150,6 +151,9 @@ class MainApp (QMainWindow): def mergePhotos(self): print "beginning photo merge" self.mergephotos.beginMerge() + def exportDocumentSlot(self, filename): + writer = ODTWriter(self.db) + writer.write(filename) if __name__ == "__main__" : diff --git a/odtwriter.py b/odtwriter.py new file mode 100644 index 0000000..ddaf83b --- /dev/null +++ b/odtwriter.py @@ -0,0 +1,63 @@ +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)