标签:wxpython activex excel 对话框 excel封装成ocx
# -*- coding: utf-8 -*- SCITE 消除乱码设置: 文件->编码->带BOM的UTF-8 # 奇怪的乱码! """ 目的: 1. 封装Excel 的 OCX太少! 好用的更是少之又少!(注意!!! iWebOffice2006.ocx涉及到版权! 这里iWebOffice2006.ocx仅做学习、研究、交流用! 如需使用请君购买正版! ) 2. OCX嵌入到wxPython对话框例子太少. 3. 这个例子就是填补1-2空白 4. 最好的利用了: 1. excel高普及率、 2. VBA操作的灵活强大 3. Python高效开发、不用编译直接运行 4. Python脚本+ VBA脚本 + 窗口界面 浑然一体 环境准备: 1. Python2.7.8 2. Office2007 3. pyInstaller2.1 4. wxPython For py2.7 5. 其他py需要的包(如:COM,Win32)根据提示安装 """ import wx import wx.lib.anchors as anchors from wx.lib.activexwrapper import MakeActiveXClass import win32com.client.gencache as win32 import sys reload(sys) de = sys.getdefaultencoding() # ascii fe = sys.getfilesystemencoding() #mbcs #print de, fe # 注意: 先使用 开始->Python2.7->PythonWin->Tools->COM Makepy utility->选中iWebOffice2006Library(1.0) # 生成C:\Python27\lib\site-packages\win32com\gen_py\19890DF8-EB54-4FB0-ABBA-5242B2A07EEEx0x1x0.py! excelControl = win32.EnsureModule('{19890DF8-EB54-4FB0-ABBA-5242B2A07EEE}',0,1,0) #print excelControl #print dir(excelControl) if excelControl is None: # 发布时要带上 19890DF8-EB54-4FB0-ABBA-5242B2A07EEEx0x1x0.py! raise ImportError("Can't load iWebOffice2006.ocx Make sure you have iWebOffice2006.ocx regstered.") [ ID_ANCHORSDEMOFRAMEANCHOREDPANEL, ID_ANCHORSDEMOFRAMEMAINPANEL, ID_ANCHORSDEMOFRAMEOKBUTTON, ID_ANCHORSDEMOFRAME, ] = map(lambda _init_ctrls: wx.NewId(), range(4)) class MyFrame(wx.Frame): def _init_utils(self): pass def _init_ctrls( self ): wx.Frame.__init__( self, size=(800, 600), id=ID_ANCHORSDEMOFRAME, title=u'用Excel(OCX)做界面', parent=None, name='AnchorsDemoFrame', style = wx.DEFAULT_FRAME_STYLE | wx.CLIP_CHILDREN, pos=(261, 123) ) self._init_utils() self.mainPanel = wx.Panel( size=(320, 160), parent=self, id=ID_ANCHORSDEMOFRAMEMAINPANEL, name='panel1', style=wx.TAB_TRAVERSAL | wx.CLIP_CHILDREN | wx.FULL_REPAINT_ON_RESIZE, pos=(0, 0) ) self.mainPanel.SetAutoLayout(True) # self.okButton = wx.Button( label='OK', id=ID_ANCHORSDEMOFRAMEOKBUTTON, parent=self.mainPanel, name='btnOk', size=(72, 24), style=0, pos=(240, 128) ) self.okButton.SetConstraints( anchors.LayoutAnchors(self.okButton, False, False, True, True) ) self.Bind( wx.EVT_BUTTON, self.OnBtnOk, id=ID_ANCHORSDEMOFRAMEOKBUTTON ) excelWrapper = MakeActiveXClass( excelControl.HandWriteCtrl ) #print excelWrapper self.excelOcx = excelWrapper( self.mainPanel, -1, (8, 40), ( 304, 80 ) ) self.excelOcx.SetConstraints( anchors.LayoutAnchors(self.excelOcx, True, True, True, True) ) self.excelOcx.ShowToolBar = 2 # 不现显示工具栏 self.excelOcx.ShowMenu = "0" # 隐藏菜单 #self.excelOcx.Compatible = False #Save Excel2007 must set false. Must befor self.excelOcx.FileType! 测试版的ocx才有此功能 self.excelOcx.FileType = ".xls" #operate excel fmt file. #print "self.excelOcx.Compatible", self.excelOcx.Compatible self.iExcelIndex = 0 self.arrExcel = [ u"./Excel报告模版/钢筋拉伸试验.xls".encode( fe ), u"./Excel报告模版/钢筋焊接件拉伸试验.xls".encode( fe ), u"./Excel报告模版/混凝土抗压试验.xls".encode( fe ), u"./Excel报告模版/混凝土抗折试验.xls".encode( fe ), u"./Excel报告模版/砂浆抗压试验.xls".encode( fe ), u"./Excel报告模版/水泥抗压试验.xls".encode( fe ), u"./Excel报告模版/砖块抗压试验.xls".encode( fe ), ] self.OpenExcelFile( self.arrExcel[ self.iExcelIndex ] ) #self.excelOcx.WebOpenLocalFile( self.arrExcel[ self.iExcelIndex ] ) # print "self.excelOcx.WebObject:", self.excelOcx.WebObject # print "self.excelOcx.WebObject.Application:", self.excelOcx.WebObject.Application #self.excelOcx.WebObject.Application.ActiveSheet.Range("AX67").Value = "陆升鹏345" self.okButton.Label = str( self.iExcelIndex ) def __init__( self ): self._init_ctrls( ) def OpenExcelFile( self, strExcelFilePath ): self.excelOcx.WebClose() # Close First self.excelOcx.WebOpenLocalFile( strExcelFilePath ) # then open #print strExcelFilePath app = self.excelOcx.WebObject.Application app.DisplayFormulaBar = False #公式 app.CommandBars[ "Cell" ].Enabled = False # 单元格 app.ActiveWindow.DisplayHeadings = False # 不显示:行头、列头 app.ActiveWindow.DisplayHorizontalScrollBar = False # 水平滚动条 app.ActiveWindow.DisplayVerticalScrollBar = True # 垂直滚动条 app.ActiveWindow.DisplayWorkbookTabs = False # 工作表标签Tab页 app.ActiveWindow.DisplayGridlines = False # 网格线 app.ActiveSheet.Protect( None, True, True, True ) # 保护单元格 def OnBtnOk( self, evt ): #r = self.excelOcx.WebSaveLocalFile( self.strExcelFile ) #print "self.excelOcx.WebSaveLocalFile:", r self.iExcelIndex += 1 self.iExcelIndex %= 5 self.OpenExcelFile( self.arrExcel[ self.iExcelIndex ] ) self.okButton.Label = str( self.iExcelIndex ) #self.excelOcx = None #self.Close() if __name__=="__main__": app=wx.PySimpleApp() media1=MyFrame() media1.Show() app.MainLoop()
利用 Excel封装成OCX 在wxPython窗口中显示、操作
标签:wxpython activex excel 对话框 excel封装成ocx
原文地址:http://blog.csdn.net/o_yale_o/article/details/43566003