Attachment 'ExcelPastedTable.py'

Download

   1 """
   2     MoinMoin wiki - Parser for Excel data copy/pasted into the edit window. 
   3     copied partly from CSV.py
   4 
   5 Installation:
   6 
   7 Copy ExcelPastedTable.py into your data/plugin/parser directory and
   8 restart your MoinMoin. 
   9 
  10 Use:
  11 
  12 put 
  13 
  14 {{{#!ExcelPastedTable
  15 
  16 }}}
  17 
  18 into your editing page
  19 
  20 then copy/paste data from excel in there like this:
  21 
  22 {{{#!ExcelPastedTable
  23 a    b    c
  24 d    e    f
  25 }}}
  26 
  27 
  28 Note:
  29 
  30 This only works if you modify Lib/site-packages/MoinMoin/parser/wiki.py 
  31 
  32 do a search for 'expandtabs' in that file and then replace the line:
  33 
  34         rawtext = self.raw.expandtabs()
  35 
  36 with this line:
  37 
  38         rawtext = self.raw
  39 
  40 I consider this a bug in MoinMoin because whatever is inside {{{#!blah }}} is not supposed
  41 to be touched except by your own parser you write. However wiki.py is getting in there
  42 and expanding the tabs. For excel-pasted data this wont work because it relies on tabs
  43 as separators. 
  44 
  45 Peace Out
  46 
  47 """
  48 
  49 class Parser:
  50 
  51     def __init__(self, raw, request, **kw):
  52         """ save incoming data """
  53         self.raw = raw
  54         self.request = request
  55         self.kw = kw
  56 
  57     def format(self, formatter):
  58         """ write html to the request object. When you do copy/
  59         paste from Excel its format is like this:
  60         a<tab>b<tab>c<tab><end of line> 
  61         d<tab>e<tab>f<tab><end of line>
  62 
  63         so i take that and use the formatter.table functions and
  64         write it out to html """
  65 
  66         lines = self.raw.replace('\r','').split('\n')
  67 
  68         self.request.write(formatter.table(1))
  69         for line in lines:
  70             if line.strip()!='':
  71                 self.request.write(formatter.table_row(1))
  72                 cells = line.split('\t')
  73                 for cell in cells:
  74                     self.request.write(formatter.table_cell(1))
  75                     self.request.write(formatter.text(cell))
  76                     self.request.write(formatter.table_cell(0))
  77                 self.request.write(formatter.table_row(0))
  78         self.request.write(formatter.table(0))
  79 
  80 
  81 if __name__=='__main__':
  82     # this is used to test this parser
  83 
  84     # fake the request object
  85     import StringIO
  86     class FakeReq(StringIO.StringIO):
  87         getText,user,cfg,current_lang,content_lang='','','','',''
  88     fakereq=FakeReq()
  89 
  90     # get a formatter object
  91     import MoinMoin.formatter.text_html
  92     testformatter = MoinMoin.formatter.text_html.Formatter(fakereq)
  93 
  94     # fake some tables as pasted from excel
  95     testtables = ['a\tb\tc\r\n' + 'd\te\tf\r\n']
  96     testtables += ['1\t2\t3\r\n' + 'd\t\t\r\n']
  97 
  98     for table in testtables:
  99         # try out the parser
 100         test = Parser(table,fakereq)
 101         test.format(testformatter)
 102         # print out result
 103         print test.request.getvalue()

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2006-02-16 14:59:57, 14.6 KB) [[attachment:Calendar-20060216.py]]
  • [get | view] (2007-05-28 09:42:03, 2.7 KB) [[attachment:ExcelPastedTable.py]]
  • [get | view] (2005-04-12 19:22:18, 6.0 KB) [[attachment:Gantt-1.3.3-2.py]]
  • [get | view] (2007-03-24 02:05:26, 3.9 KB) [[attachment:Literate_parser-0.7_Moin-1.3.tgz]]
  • [get | view] (2007-03-24 02:08:08, 4.8 KB) [[attachment:Literate_parser-0.7_Moin-1.3.zip]]
  • [get | view] (2005-03-11 13:50:49, 12.3 KB) [[attachment:MySQL.py]]
  • [get | view] (2005-09-11 08:09:44, 1.6 KB) [[attachment:SortText-1.3.5-1.py]]
  • [get | view] (2005-11-21 08:40:10, 2.8 KB) [[attachment:Sorter-1.3.py]]
  • [get | view] (2005-06-02 13:02:06, 1.2 KB) [[attachment:colorer.py]]
  • [get | view] (2006-01-04 16:10:31, 0.6 KB) [[attachment:gettext.py]]
  • [get | view] (2004-10-19 13:05:05, 0.7 KB) [[attachment:html-parser-1.2.py]]
  • [get | view] (2005-02-17 10:46:56, 0.6 KB) [[attachment:html.py]]
  • [get | view] (2005-12-06 21:09:48, 1.3 KB) [[attachment:matlab.py]]
  • [get | view] (2005-01-20 07:42:34, 0.4 KB) [[attachment:nocamelcase.py]]
  • [get | view] (2005-11-28 16:55:23, 2.3 KB) [[attachment:php-1.3.4-1]]
  • [get | view] (2005-12-18 22:36:37, 15.0 KB) [[attachment:sctable-1.3.5-4.py]]
  • [get | view] (2004-12-31 04:41:23, 1.6 KB) [[attachment:textil.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.