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.You are not allowed to attach a file to this page.