python 调用IEHtmlWindow打印网页

编写环境:

1、ActivePython-2.7.2.5-win32-x86

2、wxPython2.8-win32-unicode-2.8.12.1-py27

3、wxFormBuilder_v3.3.3-beta

 

以下代码是演示怎样利用IEHtmlWindow载入网页,并实现打印预览和打印的的功能:

#coding:utf-8
import wx
from wx.lib import iewin


class MyFrame ( wx.Frame ):

        def __init__( self, parent ):
                wx.Frame.__init__ ( self, parent, id = wx.ID_ANY, title = wx.EmptyString, pos = wx.DefaultPosition, size = wx.Size( 674,421 ), style = wx.DEFAULT_FRAME_STYLE|wx.TAB_TRAVERSAL )

                self.SetSizeHintsSz( wx.DefaultSize, wx.DefaultSize )

                self.menu = wx.MenuBar( 0 )
                self.menu1 = wx.Menu()
                self.LoadPage = wx.MenuItem( self.menu1, wx.ID_ANY, u"打开文件", wx.EmptyString, wx.ITEM_NORMAL )
                self.menu1.AppendItem( self.LoadPage )

                self.PrintPreview = wx.MenuItem( self.menu1, wx.ID_ANY, u"打印预览", wx.EmptyString, wx.ITEM_NORMAL )
                self.menu1.AppendItem( self.PrintPreview )

                self.Print = wx.MenuItem( self.menu1, wx.ID_ANY, u"打印", wx.EmptyString, wx.ITEM_NORMAL )
                self.menu1.AppendItem( self.Print )

                self.menu.Append( self.menu1, u"文件" ) 

                self.SetMenuBar( self.menu )

                bSizer = wx.BoxSizer( wx.VERTICAL )
                self.ie = iewin.IEHtmlWindow(self)
                self.ie.AddEventSink(self)
                bSizer.Add( self.ie, 1, wx.EXPAND, 5 )


                self.Centre( wx.BOTH )

                # Connect Events
                self.Bind( wx.EVT_MENU, self.LoadPageOnMenuSelection, id = self.LoadPage.GetId() )
                self.Bind( wx.EVT_MENU, self.PrintPreviewOnMenuSelection, id = self.PrintPreview.GetId() )
                self.Bind( wx.EVT_MENU, self.PrintOnMenuSelection, id = self.Print.GetId() )


        def __del__( self ):
                pass

        def DocumentComplete(self,this,pDisp,URL):
                print 'Done'
                
        # Virtual event handlers, overide them in your derived class
        def LoadPageOnMenuSelection( self, event ):
                #self.ie.Navigate("http://baidu.com/") # your URL here
                #self.ie.LoadUrl('http://www.baidu.com')
                #self.ie.LoadStream(page)
                #self.ie.LoadString(page)
                
                loadstring = u'''
                <html>
                <body>
                <p align="center"><font face="华文琥珀" size="7" color="#CC3300">这是一个简单的测试……</font></p>
                </body>
                </html>                
                '''
                self.ie.LoadString(loadstring)
                
                #import StringIO
                #self. ie.LoadStream( StringIO.StringIO( page.decode("utf8") ) )
                
                #file = open('index2.htm')
                #self. ie.LoadStream(file)
                #file.close()
                event.Skip()

        def PrintPreviewOnMenuSelection( self, event ):
                self.ie.PrintPreview()
                event.Skip()

        def PrintOnMenuSelection( self, event ):
                self.ie.Print(True)
                event.Skip()

app = wx.PySimpleApp()
frm = MyFrame(None)
frm.Maximize()
frm.Show()
app.MainLoop()

 

你可能感兴趣的:(python,打印,网页,IEHtmlWindow)