Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -*- coding: utf-8 -*-
- from win32com.client import DispatchWithEvents, Dispatch
- from time import sleep as sleep_
- import pythoncom
- recode = lambda e: e.decode('utf-8').encode('cp1251') #utf-8 -> cp1251
- supress_alert = "ОТЧЕТ ПОСТРОЕН" #supress all alerts with this string
- js_alert = """if(window["alert_"] == undefined) {
- alert_ = alert;
- alert = function(e) {
- if(e.indexOf("%s") == -1)
- alert_(e)
- }
- }""" % recode(supress_alert)
- wb_def_name = recode("Книга") #Default Excel workbook name
- class IEEvents:
- def OnDocumentComplete(self, pDisp, URL):
- pDisp = Dispatch(pDisp)
- if URL.endswith("new 2.html"):
- try:
- pDisp.Document.parentWindow.execScript(js_alert)
- print "Info. Alerts supressed on", URL
- except Exception as e:
- if e.excepinfo[-1] & 0xffffffff == 0x80070005:
- print "Error. Internet explorer blocked content."
- else: print e
- # ie = DispatchWithEvents("InternetExplorer.Application", IEEvents)
- # ie.Visible = 1
- def sleep(sec):
- sleep_(sec)
- pythoncom.PumpWaitingMessages()
- def waitForWorkbook():
- print "Info. Waiting for workbook..."
- context, rot = pythoncom.CreateBindCtx(0), pythoncom.GetRunningObjectTable()
- found, hashes = False, []
- while not found:
- for i in rot:
- hash = i.Hash()
- if hash not in hashes:
- hashes.append(hash)
- print "checking", i.GetDisplayName(context, None)
- if i.GetDisplayName(context, None).startswith(wb_def_name):
- obj = rot.GetObject(i)
- wb = Dispatch(obj.QueryInterface(pythoncom.IID_IDispatch))
- if not wb.Parent.Visible:
- print "Info. Workbook found -", wb.Name
- found = True
- break
- sleep(0.05)
- print "Info. Waiting for script to finish..."
- while not wb.Parent.Visible:
- sleep(0.05)
- return wb
- wb = waitForWorkbook()
- print "Now save workbook", wb.Name
Advertisement
Add Comment
Please, Sign In to add comment