Use Excel to encapsulate an OCX in the Wxpython window to display, manipulate

Source: Internet
Author: User

#-*-Coding:utf-8-*-scite Eliminate garbled settings: file-, code-, with BOM utf-8# strange garbled! "" Purpose: 1. Encapsulate Excel with too few OCX! Easy to use and less! (Note!!! Iweboffice2006.ocx related to copyright! Here Iweboffice2006.ocx only do study, research, exchange with! If you want to use the purchase of genuine! ) 2.    There are too few examples of OCX embedding into the Wxpython dialog box. 3. This example is to fill 1-2 blank 4. The best use: 1. Excel high Penetration, 2. VBA operation is flexible and powerful 3. Python is developed efficiently and runs without compiling 4 directly. Python script + VBA script + window interface Seamless Environment preparation: 1. Python2.7.8 2. Office2007 3. pyInstaller2.1 4. WxPython for py2.7 5. Other packages required by PY (e.g. Com,win32) install "" "Import wximport wx.lib.anchors as Anchorsfrom wx.lib.activexwrapper import as prompted Makeactivexclassimport Win32com.client.gencache as Win32import sysreload (sys) de = sys.getdefaultencoding () # Asciife = Sys.getfilesystemencoding () #mbcs #print de, fe# Note: First Use start->python2.7->pythonwin->tools->com Makepy Utility-> Check Iweboffice2006library (1.0) # Generate 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: # when released    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_ANCHORSDEMO FRAME,] = 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= (), Id=id_anchorsdemoframe, Title=u ' interface with 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= (), parent=self, ID         =id_anchorsdemoframemainpanel,                    Name= ' Panel1 ', style=wx. Tab_traversal | Wx. Clip_children | Wx. Full_repaint_on_resize, pos= (0, 0)) Self.mainPanel.SetAutoL Ayout (True) # Self.okbutton = wx.                             Button (label= ' OK ', Id=id_anchorsdemoframeokbutton, Parent=self.mainpanel, name= ' Btnok ', size= (,), S Tyle=0, pos= () 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, +), (304,)) selF.excelocx.setconstraints (anchors.        Layoutanchors (Self.excelocx, True, True, True, true)) Self.excelOcx.ShowToolBar = 2 # show Toolbars Now Self.excelOcx.ShowMenu = "0" # hidden menu #self. excelocx.compatible = False #Save Excel2007 must set False. Must befor self.excelocx.filetype!                The beta version of OCX has this feature self.excelOcx.FileType = ". xls" #operate the Excel fmt file. #print "Self.excelOcx.Compatible", self.excelOcx.Compatible self.iexcelindex = 0 Self.arrexcel = [u                           "./excel report Template/rebar tensile test. xls". Encode (Fe), U "./excel report Template/Rebar weld tensile test. xls". Encode (Fe),                           U "./excel report Template/Concrete compression test. xls". Encode (Fe), U "./excel report Template/Concrete flexural test. xls". Encode (Fe),  U "./excel report Template/mortar compression test. xls". Encode (Fe), U "./excel report Template/Cement compression test. xls". Encode ( Fe), u "./excel report Template/Brick compression test. 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.Applicat Ion: "Self.excelOcx.WebObject.Application #self. ExcelOcx.WebObject.Application.ActiveSheet.Range (" AX67 "). Value = "Luzerne Peng 345" Self.okButton.Label = str (SELF.IEXCELINDEX) def __init__ (self): Self._init_ctrls () def openexcelfile (self, Strexcelfilepath): Self.excelOcx.WebClose () # Close First Self.exc Elocx.webopenlocalfile (Strexcelfilepath) # then open #print strexcelfilepath app = Self.excelOcx.WebObject . Application app. Displayformulabar = False #公式 app.commandbars["Cell"]. Enabled = False # cell app. Activewindow.displayheadings = False # does not display: wardrobe, column-header app. Activewindow.displayhorizontalscrollbar = False # horizontal scroll bar APp. Activewindow.displayverticalscrollbar = True # vertical scroll bar app. Activewindow.displayworkbooktabs = False # sheet tab tab app. Activewindow.displaygridlines = False # gridlines App. ActiveSheet.Protect (None, True, True, true) # protection cell def Onbtnok (self, evt): #r = Self.excelOcx.WebS        Avelocalfile (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) #s Elf.excelocx = None #self. Close () If __name__== "__main__": app=wx. Pysimpleapp () Media1=myframe () media1. Show () app.  Mainloop ()

Use Excel to encapsulate an OCX in the Wxpython window to display, manipulate

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.