[Mpuls-commits] r5508 - base/trunk/mpulsweb/lib

scm-commit@wald.intevation.org scm-commit at wald.intevation.org
Fri Oct 28 13:46:28 CEST 2011


Author: bricks
Date: 2011-10-28 13:46:27 +0200 (Fri, 28 Oct 2011)
New Revision: 5508

Modified:
   base/trunk/mpulsweb/lib/export.py
Log:
Format date as date in excel export

Set a cell style for date format. It seems that the format can
be any possible YY DD MM format. Now the format DD.MM.YYYY is used
by default. The cell is recognized as date format in MS Office 2003.


Modified: base/trunk/mpulsweb/lib/export.py
===================================================================
--- base/trunk/mpulsweb/lib/export.py	2011-10-28 08:54:36 UTC (rev 5507)
+++ base/trunk/mpulsweb/lib/export.py	2011-10-28 11:46:27 UTC (rev 5508)
@@ -4,6 +4,7 @@
 import zipfile
 import stat
 from collections import defaultdict
+from datetime import datetime, date
 
 import pyExcelerator
 
@@ -180,14 +181,16 @@
 
         for row_num, row in enumerate(self.rows):
             for column, name in enumerate(self.columns):
-                sheet.write(row_num + 1, column, self.encode(row.get(name)))
+                value, style = self.encode(row.get(name))
+                sheet.write(row_num + 1, column, value, style)
 
     def encode(self, value):
+        style = pyExcelerator.XFStyle()
         if value is None:
-            return ""
+            return ("", style)
         if isinstance(value, basestring) and len(value) > 32765:
             value = value[:32765]
-        return value
+        return (value, style)
 
 
 class XLSContainer(Container):
@@ -283,13 +286,19 @@
 class FormletterXLSTable(XLSTable):
 
     def encode(self, value):
+        style = pyExcelerator.XFStyle()
         if value is None:
-            return ""
+            return ("", style)
+        if isinstance(value, (date, datetime)):
+            #style.num_format_str = "M/D/YY"
+            style.num_format_str = "DD.MM.YYYY"
+            value = value.strftime("%d.%m.%Y")
+            return (value, style)
         if isinstance(value, basestring) and len(value) > 32765:
             value = value[:32765]
         if not isinstance(value, basestring):
-            return str(value)
-        return value
+            return (str(value), style)
+        return (value, style)
 
 class FormletterExport(Export):
 



More information about the Mpuls-commits mailing list