File: XLSGrid.py

package info (click to toggle)
wxpython3.0 3.0.2.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 482,760 kB
  • ctags: 518,293
  • sloc: cpp: 2,127,226; python: 294,045; makefile: 51,942; ansic: 19,033; sh: 3,013; xml: 1,629; perl: 17
file content (236 lines) | stat: -rw-r--r-- 7,206 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
import os
import sys
import wx

try:
    dirName = os.path.dirname(os.path.abspath(__file__))
except:
    dirName = os.path.dirname(os.path.abspath(sys.argv[0]))

sys.path.append(os.path.split(dirName)[0])

_isStandalone = False

try:
    from agw import xlsgrid as XG
    _isStandalone = True
except ImportError: # if it's not there locally, try the wxPython lib.
    import wx.lib.agw.xlsgrid as XG

dataDir = os.path.join(dirName, "data")

_hasXLRD = True

try:
    import xlrd
except ImportError:
    _hasXLRD = False

_hasWin32 = False

if wx.Platform == "__WXMSW__":
    try:
        from win32com.client import Dispatch
        _hasWin32 = True
    except ImportError:
        pass


_msg = "This is the about dialog of the XLSGrid demo.\n\n" + \
       "Author: Andrea Gavana @ 17 Aug 2011\n\n" + \
       "Please report any bugs/requests of improvements\n" + \
       "to me at the following addresses:\n\n" + \
       "andrea.gavana@gmail.com\n" + "andrea.gavana@maerskoil.com\n\n" + \
       "Welcome to wxPython " + wx.VERSION_STRING + "!!"


class XLSGridDemo(wx.Panel):

    def __init__(self, parent, log):

        wx.Panel.__init__(self, parent, -1)

        b = wx.Button(self, -1, " Test XLSGrid ", (50, 50))
        self.Bind(wx.EVT_BUTTON, self.OnButton, b)


    def OnButton(self, evt):

        XLSGridFrame(None)        


class XLSGridFrame(wx.Frame):

    def __init__(self, parent, size=(950, 730)):

        wx.Frame.__init__(self, parent, title="XLSGrid wxPython Demo", size=size)
        panel = XLSGridPanel(self)

        self.CreateMenuAndStatusBar()
        self.CenterOnScreen()
        self.Show()


    def CreateMenuAndStatusBar(self):

        menuBar = wx.MenuBar()
        fileMenu = wx.Menu()
        helpMenu = wx.Menu()
        
        item = wx.MenuItem(fileMenu, wx.ID_ANY, "E&xit", "Exit XLSGrid demo")
        self.Bind(wx.EVT_MENU, self.OnClose, item)
        fileMenu.AppendItem(item)

        item = wx.MenuItem(helpMenu, wx.ID_ANY, "About...", "Shows the about dialog")
        self.Bind(wx.EVT_MENU, self.OnAbout, item)
        helpMenu.AppendItem(item)
        
        menuBar.Append(fileMenu, "&File")
        menuBar.Append(helpMenu, "&Help")

        self.SetMenuBar(menuBar)

        statusbar = self.CreateStatusBar(2, wx.ST_SIZEGRIP)
        statusbar.SetStatusWidths([-2, -1])
        
        statusbar_fields = [("wxPython XLSGrid Demo, Andrea Gavana @ 08 Aug 2011"),
                            ("Welcome To wxPython!")]
        
        for i in xrange(len(statusbar_fields)):
            statusbar.SetStatusText(statusbar_fields[i], i)


    def OnClose(self, event):

        wx.CallAfter(self.Destroy)


    def OnAbout(self, event):

        dlg = wx.MessageDialog(self, _msg, "XLSGrid wxPython Demo",
                               wx.OK | wx.ICON_INFORMATION)
        dlg.ShowModal()
        dlg.Destroy()


class XLSGridPanel(wx.Panel):

    def __init__(self, parent):

        wx.Panel.__init__(self, parent)
        
        self.start_button = wx.Button(self, -1, "Start")
        self.grid = XG.XLSGrid(self)

        self.grid.Hide()        

        self.DoLayout()

        self.Bind(wx.EVT_BUTTON, self.OnStart, self.start_button)


    def DoLayout(self):

        xlrd_ver = xlrd.__VERSION__
        string_xlrd = "Version " + xlrd_ver
        
        if xlrd_ver <= "0.7.1":
            string_xlrd += ": hyperlink and rich-text functionalities will not work. xlrd 0.7.2 (SVN) is required for this."
        else:
            string_xlrd += ": hyperlink and rich-text functionalities will work!"

        if _hasWin32:
            string_pywin32 = "You have pywin32! XLSGrid cells should appear exactly as in Excel (WYSIWYG)."
        else:
            string_pywin32 = "You don't have pywin32. Cell string formatting will be severely limited."
            
        main_sizer = wx.BoxSizer(wx.VERTICAL)
        top_sizer = wx.BoxSizer(wx.HORIZONTAL)
        top_right_sizer = wx.BoxSizer(wx.VERTICAL)
        top_center_sizer = wx.BoxSizer(wx.VERTICAL)
        top_sizer.Add(self.start_button, 0, wx.ALL|wx.ALIGN_CENTER_HORIZONTAL|wx.ALIGN_CENTER_VERTICAL, 10)
        label_1 = wx.StaticText(self, -1, "xlrd:")
        label_1.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
        top_center_sizer.Add(label_1, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 5)
        top_center_sizer.Add((0, 0), 1, wx.EXPAND, 0)
        label_2 = wx.StaticText(self, -1, "pywin32:")
        label_2.SetFont(wx.Font(8, wx.DEFAULT, wx.NORMAL, wx.BOLD, 0, ""))
        top_center_sizer.Add(label_2, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL|wx.ALIGN_RIGHT, 5)
        top_sizer.Add(top_center_sizer, 0, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 0)
        label_xlrd = wx.StaticText(self, -1, string_xlrd)
        top_right_sizer.Add(label_xlrd, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
        top_right_sizer.Add((0, 0), 1, wx.EXPAND, 0)
        label_pywin32 = wx.StaticText(self, -1, string_pywin32)
        top_right_sizer.Add(label_pywin32, 0, wx.ALL|wx.ALIGN_CENTER_VERTICAL, 5)
        top_sizer.Add(top_right_sizer, 1, wx.EXPAND|wx.ALIGN_CENTER_VERTICAL, 0)
        main_sizer.Add(top_sizer, 0, wx.ALL|wx.EXPAND, 5)
        main_sizer.Add((0, 10))
        main_sizer.Add(self.grid, 1, wx.ALL|wx.EXPAND, 5)
        self.SetSizer(main_sizer)

        main_sizer.Layout()
        

    def OnStart(self, event):

        event.Skip()
        
        filename = os.path.join(os.path.abspath(dataDir), "Example_1.xls")
        
        if not os.path.isfile(filename):
            dlg = wx.MessageDialog(self, 'Error: the file "Example_1.xls" is not in the "data" directory',
                                   'XLSGridDemo Error', wx.OK | wx.ICON_ERROR)
            dlg.ShowModal()
            dlg.Destroy()
            return

        busy = wx.BusyInfo("Reading Excel file, please wait...")
        
        sheetname = "Example_1"
        book = xlrd.open_workbook(filename, formatting_info=1)

        sheet = book.sheet_by_name(sheetname)
        rows, cols = sheet.nrows, sheet.ncols

        comments, texts = XG.ReadExcelCOM(filename, sheetname, rows, cols)

        del busy

        self.grid.Show()
        self.grid.PopulateGrid(book, sheet, texts, comments)
        
        self.start_button.Enable(False)
        self.Layout()


#----------------------------------------------------------------------

def runTest(frame, nb, log):

    if _hasXLRD:
        win = XLSGridDemo(nb, log)
        return win
    else:
        msg = 'This demo requires the xlrd package to be installed.\n' \
              'See: http://pypi.python.org/pypi/xlrd'
        
        if _isStandalone:
            dlg = wx.MessageDialog(nb, msg, 'Sorry', wx.ICON_WARNING|wx.OK)
            dlg.ShowModal()
            dlg.Destroy()
            return None
        else:
            from Main import MessagePanel
            win = MessagePanel(nb, msg, 'Sorry', wx.ICON_WARNING)
            
        return win
    
#----------------------------------------------------------------------

overview = XG.__doc__

if __name__ == '__main__':
    import sys,os
    import run
    run.main(['', os.path.basename(sys.argv[0])] + sys.argv[1:])