-4

I need to create a dialog that will display an HTML page. The application is written in Python and the dialog must contain Internet Explorer control. Does Python support integration with COM/ActiveX and are there any working examples of such integration?

If possible please provide an answer without links to third-party websites. Please do not offer .NET/C++/Chrome or any other combinations. Python+IE is my requirement and can not be changed.

IT Hit WebDAV
  • 5,652
  • 12
  • 61
  • 98
  • 5
    Questions asking us to **recommend or find a book, tool, software library, tutorial or other off-site resource** are off-topic for Stack Overflow as they tend to attract opinionated answers and spam. You should know that by now... – MattDMo Mar 22 '16 at 20:29

2 Answers2

5

You can embed a Internet Explorer window within a Python GUI application by using the wxWidgets WebView component (via wxPython). Unlike e.g. Qt that includes a standardised browser component across all platforms, wxWidgets uses the native browser on the host platform by default. You can also force IE (ignoring the user's default browser) by setting the backend manually.

In the documentation it shows WEBVIEW_BACKEND_IE, but the actual value is available via wx.html2.WebViewBackendIE. The following is a complete working example, adapted from here:

import wx 
import wx.html2 

class MyBrowser(wx.Frame): 
    def __init__(self, *args, **kwds): 
        wx.Frame.__init__(self, *args, **kwds) 
        sizer = wx.BoxSizer(wx.VERTICAL) 
        self.browser = wx.html2.WebView.New(self, backend=wx.html2.WebViewBackendIE) 
        sizer.Add(self.browser, 1, wx.EXPAND, 10) 
        self.SetSizer(sizer) 
        self.SetSize((700, 700)) 

if __name__ == '__main__': 
    app = wx.App() 
    dialog = MyBrowser(None, -1) 
    dialog.browser.LoadURL("https://www.google.com") 
    dialog.Show() 
    app.MainLoop() 

Of course, this will only work on Windows. For more information on WebViews in wxWidgets see the documentation.

If you are already using (Py)Qt for your application, Qt also supports embedding of ActiveX objects in windows using the QAxWidget class. There is a web browser example in the Qt documentation. Below is a minimal PyQt4 example:

browser = QAxContainer.QAxWidget() 
browser.setControl("<ActiveX ID>") # e.g. "{8856F961-340A-11D0-A96B-00C04FD705A2}"
browser.dynamicCall('Navigate(const QString&)', QtCore.QString("google.com"))
Community
  • 1
  • 1
mfitzp
  • 15,275
  • 7
  • 50
  • 70
2

You can use COM without much difficulty from Python. Here is a simple example:

import win32com.client
ie = win32com.client.Dispatch("InternetExplorer.Application")

ie.Visible = 1
ie.Navigate('www.google.com')

You may just want to download this project and read through the IEC.py to get all your com commands out that are helpful (I know you weren't looking for links, but in this case, there are already 500 lines of code that could save you a lot of time, button clicks, get document text, checkboxes, submit forms, etc.) http://www.mayukhbose.com/python/IEC/

I think that will solve your problem. If you already know all your IE com commands (or just hit "tab" in IPython to get a list In [1]: ie. <tab>), then the above simple example should be sufficient.

Also note when using COM objects you can often speed them up considerably by just running this:

from win32com.client import makepy
makepy.main()

Then scroll through the pop up window and find your application (Microsoft Internet Controls for IE) - all the registered COM interfaces should be there, this will autogenerate the internal methods instead of doing it every time you call the library.

Matt
  • 2,602
  • 13
  • 36