Hi again, To create an instance of the interface use:
ie = Dispatch('InternetExplorer.Application') # To get a document and body have to navigate ie.Navigate('about:') doc = ie.Document body = doc.body body.write('<h1 id=mytitle>Hello there</h1>') ele = body.all('mytitle') To catch events see the code in MainWindow.py. It doesn't work because it's part of a bigger project, but you can see how things work. Also another really good way of embedding IE is in a wxPython application. In the wxPython demo application see the file ActiveXWrapper_IE.py GBU Matthew Sherborne Use the attached files to play with: ----- Original Message ----- From: "Bill Bell" <[EMAIL PROTECTED]> To: <[EMAIL PROTECTED]> Sent: Monday, February 04, 2002 2:47 PM Subject: Re: Using MSHTML in Python Matthew, thanks. I had given up on that problem. Given that Mark Hammond seemed to be saying that what I wanted is impossible, I can hardly wait to try your code out. Thanks again. Bill *********** REPLY SEPARATOR *********** On 2/4/2002 at 1:18 PM Matthew wrote: >I saw your message on the net. Hope this helps... > >from win32com.client import Dispatch, getevents, gencache >gencache.EnsureModule(pythoncom.MakeIID('{EAB22AC0-30C1-11CF-A7EB-0000C05BA E0B}'), 0x0, 1, 1) >gencache.EnsureModule(pythoncom.MakeIID('{3050F1C5-98B5-11CF-BB82-00AA00BDC E0B}'), 0x0, 4, 0) > >:) > >__ORIGINAL_MESSAGE___ > >I'm trying to do the equivalent of the MS 'walkall' example, in >Python. Which would explain why I would use a statement like the >following: > >MSHTMLIDispatch, MSHTMLIOleObject, MSHTMLIOleControl = >CoCreateInstanceEx ( 'htmlfile', None, pythoncom . >CLSCTX_INPROC_SERVER, None, ( pythoncom . IID_IDispatch, >axcontrol . IID_IOleObject, axcontrol . IID_IOleControl, ) ) > >This works, at least in the sense that Python does not complain. (I >haven't proceeded far enough to verify that MSHTML can >successfully query my host for the parameters within it must work.) > >For those unfamiliar with the CoClass represented by 'htmlfile', it >offers several Interfaces, including 'IHTMLDocument2'. > >However, when I enter the following statement: > >MSHTMLIDispatch.QueryInterface ( '{332C4425-26CB-11D0-B483- >00C04FD90119}' ) > >Python complains, "TypeError: There is no interface object >registered that supports this IID." > >Notes: > >1. In fact, 'IHTMLDocument2' is the '(default)' for the key >HKEY_CLASSES_ROOT\Interface\{332C4425-26CB-11D0-B483- >00C04FD90119}. > >2. CoCreateInstance( CLSID_HTMLDocument, NULL, >CLSCTX_INPROC_SERVER, IID_IHTMLDocument2, >(LPVOID*)&g_pApp -> m_pMSHTML ))) from 'walkall' uses these >constants for the IDs and yields something useful. > >Two items I'd appreciate comment about (or, IOW, please ease my >torment): > >1. How to retrieve an artbitrary interface supported by a CoClass >given some one of the elements in the tuple returned by >CoCreateInstanceEx. > >2. Why doesn't QI cut it for me in this case? > >Thanks very much! Ou merci beaucoups! > >Bill > >Bill Bell, Software Developer
from win32com.client import * cast = gencache.GetModuleForProgID('htmlfile') cast2 = gencache.GetModuleForProgID('InternetExplorer.Application') def _createDiv(): div = doc3.createElement('div') style = div.style style2 = cast.IHTMLStyle2(style) style3 = cast.IHTMLStyle3(style) style2.position = 'absolute' style.top = 0 style.left = 0 style.overflow = 'scroll' style2.overflowX = 'hidden' style.width = 300 style.height = 100 style3.scrollbarBaseColor = '#3366CC' style.borderBottom = '2px solid black' style3.scrollbarHighlightColor = '#99CCFF' style3.scrollbarArrowColor = 'white' div.innerHTML = 'Hello' return div b = Dispatch('InternetExplorer.Application') b.Navigate('about:<h1 id=header>Hello</h1><iframe id=frm src="about:"></iframe>') b.Visible = 1 doc1 = cast.IHTMLDocument2(b.Document) header = doc1.all.item('header') frm = doc1.all.item('frm') frm2 = doc1.frames('frm') doc2 = cast.IHTMLDocument2(frm2.document) div = doc2.createElement('div') cast.DispHTMLBody(doc2.body).appendChild(div) popup = cast.DispHTMLWindow2(doc1.parentWindow).createPopup() doc3 = cast.IHTMLDocument2(popup.document) body = cast.DispHTMLBody(doc3.body) div = _createDiv()
from win32com.client import getevents, gencache from Area import Area import traceback, pdb from DesignPanel import DesignPanel from exceptions import Exception cast = gencache.GetModuleForProgID('htmlfile') # We'll need the actual python module that defines the html document, so we can type cast somethings DocEvents = getevents('htmlfile') # Get the document events object, we're going to inherit from it. class MainWindow(DocEvents): def __init__(self, doc): DocEvents.__init__(self, doc) f = open("mainwindowres.html", "r") # Rather than navigate to the URL, I decided to HTML = f.read() # to load it stright into a string, and dump it into f.close() # the borwser's document self.doc = doc doc.write(HTML) # Get some quick references to some of the elements in the document self.btnAdd = doc.all('btnAdd') self.btnDelete = doc.all('btnDelete') self.info = doc.all('info') self.statusBar = doc.all.item('statusBar') self.designPanel = DesignPanel(self) # Make content editable doc.execCommand('2D-Position', 0, 1) # This allows the user to move children of editable elements around def Ononclick(self): """ Called when any element on the document is clicked, used only to dispatch the event. Also remembers the last area element to be clicked.""" self.event = event = self.doc.parentWindow.event # Get the event object if not event: try: self.designPanel.handleClick() # No event means that it's for the designPanel except Exception: traceback.print_exc() ele = event.srcElement # Get a reference to the element that was clicked if ele and ele.id: # Only elements with id's will be dispatched attrName = ele.id + 'Clicked' # Search for a method under 'self'. eg. 'btnAddClicked' if hasattr(self, attrName): try: getattr(self, attrName)() # Call the method except Exception: trabceback.print_exc() # Inside the message loop, things just keep going! def Ononcontextmenu(self): """ Stops the popup menu for everything. Won't be reached by area element popups """ # Was it our document that was clicked, or the designpanel? event = self.doc.parentWindow.event or self.designPanel.doc.parentWindow.event if event: event.returnValue = 0 def btnAddClicked(self): """Called when btnAdd is clicked, the next click in the design panel will place a rectangular area there""" if not self.designPanel.addingRect: self.designPanel.addingRect = 1 self.info.innerText = 'Adding a rectanglular area' self.btnAdd.style.backgroundColor = 'yellow' else: self.designPanel.addingRect = 0 self.info.innerText = '' self.btnAdd.style.backgroundColor = 'ivory' def btnDeleteClicked(self): """Removes the last selected area""" self.designPanel.deleteSelectedArea() def btnSaveClicked(self): HTML = self.designPanel.doc.body.innerHTML f = open('test.html', 'w') f.write(HTML) f.close() if __name__ == "__main__": import app app.main()