Not logged in. · Lost password · Register
Forum: Support Ideas and suggestions RSS
Export (ODT)
YvesFischer #1
Member since Feb 2006 · 11 posts · Location: South West Germany
Group memberships: Members
Show profile · Link to this post
Subject: Export (ODT)
Für eine einfache Archivierung eines Forums habe ich mir einen einfachen ODT-Export geschrieben. Es ist nicht perfekt (kein ABBC-Code wird aufgelöst, die Generierung des Inhaltsverzeichnisses muss in OOo angestossen werden) und hat sicher jede Menge Fehler aber vielleicht hilft es jemand weiter.

(abhängigkeit: odfpy, python-mysqldb)
  1. #!/usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import MySQLdb
  4. from odf.opendocument import OpenDocumentText
  5. from odf.teletype import WhitespaceText
  6. from odf.style import Style, TextProperties, ParagraphProperties
  7. from odf.text import H, P, Span, TableOfContent, TocMark
  8.  
  9.  
  10.  
  11. dbhost = raw_input("db host[127.0.0.1]") or "127.0.0.1"
  12. prefix = raw_input("db prefix[unb_tg]: ") or "unb_tg"
  13. dbuser = raw_input("db user[yvesf]: ") or "yvesf"
  14. dbname = raw_input("db name[%s]: "%dbuser) or dbuser
  15. dbpass = raw_input("Password[]: ") or ""
  16. def debug(str):
  17.     print str
  18. def recode(str):
  19.     return unicode(str, 'cp1252')
  20.  
  21. class Forum(object):
  22.     def __init__(self,db):
  23.         self.forums = {}
  24.         self.db = db
  25.         self._build_forum_tree(node=self.forums,id=0)
  26.  
  27.     def _build_forum_tree(self,node,id):
  28.         debug("_build_forum_tree(id=%s)"%id)
  29.         cur = self.db.cursor()
  30.         cur.execute("SELECT ID, Parent,Name,Description FROM %s_Forums WHERE Parent = %s ORDER BY SORT" % (prefix,id))
  31.         for row in cur.fetchall():
  32.             node[row[0]] = {}
  33.             self._build_forum_tree(node[row[0]], row[0])
  34.         cur.close()
  35.  
  36.     @staticmethod
  37.     def get_forum_info(db,forumID):
  38.         cur = db.cursor()
  39.         cur.execute("SELECT * FROM %s_Forums WHERE ID = %s" %(prefix,forumID))
  40.         row = cur.fetchone()
  41.         return {"ID" : row[0], "Parent" : row[1], "Name" : row[3], "Description" : row[5]}
  42.  
  43. class Thread(object):
  44.     def __init__(self,db,forumID):
  45.         self.db = db
  46.         self.forumID = forumID
  47.         self.threads = []
  48.         cur = self.db.cursor()
  49.         cur.execute("SELECT ID, Forum, FROM_UNIXTIME(LastPostDate), Subject, t.Desc, FROM_UNIXTIME(Date) FROM %s_Threads t WHERE Forum = %s ORDER BY Date" % (prefix,forumID))
  50.         for row in cur.fetchall():
  51.             self.threads.append(row)
  52.         cur.close()
  53.     @staticmethod
  54.     def get_posts(db,thread_id):
  55.         cur = db.cursor()
  56.         cur.execute("""SELECT u.Name, p.Subject, p.Msg, FROM_UNIXTIME(p.Date)
  57.         FROM %s_Posts p INNER JOIN %s_Users u
  58.             ON u.ID = p.User
  59.         WHERE p.Thread = %s""" % (prefix, prefix, thread_id))
  60.         return cur.fetchall()
  61.  
  62. class Document(object):
  63.     def __init__(self,db):
  64.         self.db = db
  65.         self.doc = OpenDocumentText()
  66.    
  67.         self.st_forum_description = Style(name="Forum Description", family="paragraph")
  68.         self.st_forum_description.addElement(TextProperties(attributes={'fontsize':'16pt'}))
  69.         self.doc.styles.addElement(self.st_forum_description)
  70.        
  71.         self.st_forum_title = Style(name="Forum Title", family="paragraph")
  72.         self.st_forum_title.addElement(TextProperties(attributes={'fontsize':'23pt','fontweight':'bold'}))
  73.         self.doc.styles.addElement(self.st_forum_title)
  74.  
  75.         self.st_thread_title = Style(name="Thread title", family="paragraph")
  76.         self.st_thread_title.addElement(TextProperties(attributes={'fontsize':'15pt', 'fontweight':'bold'}))
  77.         self.doc.styles.addElement(self.st_thread_title)
  78.  
  79.         self.st_thread_description = Style(name="Thread Description", family="paragraph")
  80.         self.st_thread_description.addElement(TextProperties(attributes={'fontsize':'14pt'}))
  81.         self.doc.styles.addElement(self.st_thread_description)
  82.        
  83.         self.st_post_header = Style(name="Post Header", family="paragraph")
  84.         self.st_post_header.addElement(TextProperties(attributes={'fontweight':'bold'}))
  85.         self.doc.styles.addElement(self.st_post_header)
  86.  
  87.         self.st_post_body = Style(name="Post Body", family="paragraph")
  88.         self.st_post_body.addElement(TextProperties(attributes={'fontsize':'11pt'}))
  89.         self.st_post_body.addElement(ParagraphProperties(attributes={'marginbottom':'1cm', 'margintop':'0.5cm'}))
  90.         self.doc.styles.addElement(self.st_post_body)
  91.  
  92.         self.doc.text.addElement(TableOfContent(attributes={'name':'Inhalt'}))
  93.  
  94.  
  95.  
  96.     def line_break_escape(self,str):
  97.         return str.replace(u'\n', u'<text:line-break/>').replace(u'\r', u'<text:line-break/>')
  98.     def parse_forum_tree(self,tree,indent=0):
  99.         for subforum_key in tree.keys():
  100.             forum = Forum.get_forum_info(self.db,subforum_key)
  101.             p = P(stylename=self.st_forum_title, text=u"%s> Forum %s" % (indent,recode(forum["Name"])))
  102.             p.addElement(TocMark(attributes={'stringvalue':u'%s'%recode(forum["Name"]), 'outlinelevel':'%s'%indent}))
  103.             self.doc.text.addElement(p)
  104.             self.doc.text.addElement(P(stylename=self.st_forum_description, text=u"%s" % (recode(forum["Description"]))))
  105.             for thread in Thread(self.db, subforum_key).threads:
  106.                 p=P(stylename=self.st_thread_title, text=u"Thread: %s" % (recode(thread[3])))
  107.                 p.addElement(TocMark(attributes={'stringvalue':u'%s'%recode(thread[3]), 'outlinelevel':(u'%s'%(indent+1))}))
  108.                 self.doc.text.addElement(p)
  109.                 self.doc.text.addElement(P(stylename=self.st_thread_description, text=u"Beschreibung: %s, Start: %s" % (recode(thread[4]) or u"<leer>",recode(thread[5].__str__()))))
  110.                 for post in Thread.get_posts(self.db, thread[0]):
  111.                     self.doc.text.addElement(P(stylename=self.st_post_header, text=u"%s schrieb am %s" % (recode(post[0]), recode(post[3].__str__()))))
  112.                     w = WhitespaceText()
  113.                     p = P(stylename=self.st_post_body)
  114.                     w.addTextToElement(p, recode(post[2]))
  115.  
  116.                     self.doc.text.addElement(p)
  117.                     print "P",
  118.                 print "T",
  119.  
  120.             self.parse_forum_tree(tree[subforum_key],indent+1)
  121.  
  122.     def save(self,filename):
  123.         self.doc.save(filename,True)
  124.            
  125. con = MySQLdb.connect(
  126.     host = dbhost,
  127.     db = dbname,
  128.     user = dbuser,
  129.     passwd = dbpass)
  130.  
  131.  
  132. f = Forum(con)
  133. doc = Document(con)
  134. doc.parse_forum_tree(f.forums)
  135. doc.save("out")
Close Smaller – Larger + Reply to this post:
Verification code: VeriCode Please enter the word from the image into the text field below. (Type the letters only, lower case is okay.)
Smileys: :-) ;-) :-D :-p :blush: :cool: :rolleyes: :huh: :-/ <_< :-( :'( :#: :scared: 8-( :nuts: :-O
Special characters:
Go to forum
This board is powered by the Unclassified NewsBoard software, 20110527-dev, © 2003-2011 by Yves Goergen
Page created in 227.4 ms (140.4 ms) · 33 database queries in 133.5 ms
Current time: 2012-02-08, 09:20:47 (UTC +01:00)