Automatisierung von Windows-Anwendungen

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Notes on slide 1

    @pycologne Vortrag Automatisierung von Windows-Anwendungen

    Demo: Slide change notification from Microsoft PowerPoint. If you read this message, the demo is successful!

    In Python, its very easy to catch the slide change event. Full source code on http://pastebin.ca/1478907

    For details see Roy Hans #pycon tutorial Automating Windows Applications with win32com http://tr.im/q43o

    Good tutorial for Automating Windows Applications with win32com by Roy Han: http://tr.im/q43o

    Book: Python Programming on Win32 http://tr.im/vZlM

    Favorites, Groups & Events

    Automatisierung von Windows-Anwendungen - Presentation Transcript

    1. Automatisierung von Windows-Anwendungen Python for Windows PyCologne (12.08.2009, Köln) Andreas Schreiber <Andreas.Schreiber@dlr.de> http://andreas-schreiber.net | http://www.pycologne.de
    2. Automatisierung von Windows-Anwendungen Um was geht es?
      • Steuerung und Erweiterung existierender Windows-Applikationen
      • Applikationen mit COM-Interface
      • Client-Skripte in Python
      • Event-Handling
      • Add-Ins
      • Weitere Themen (hier nicht behandelt)
      • System-Programmierung unter Windows
      • Erstellung von Windows-Services mit Python
      • GUI-Programmierung
      • File-Management
      Modul win32com Module win32/*
    3. COM Component Object Model
      • Plattform-Technologie für Windows
      • Interprozesskommunikation
      • Dynamische Objekterzeugung
      • Client/Server-Architektur
      • COM-Client instanziert eine COM-Komponente in einem COM-Server und nutzt die Funktionalität des Objektes über COM-Interfaces
      • COM-Server ist eine DLL oder ein Executable
    4. COM COM-Interface
      • COM-Komponente kann dazu über allgemeine Schnittstellen und über spezielle Schnittstellen angesprochen werden.
      • Jedes Interface hat eine weltweit eindeutige Identifikationsnummer, die GUID (Globally Unique Identifier)
      // Standardschnittstelle aller COM-Komponenten [ object, uuid(00000000-0000-0000-C000-000000000046) ] interface IUnknown { [restricted] HRESULT _stdcall QueryInterface([in] GUID* rrid, [out] void** ppvObj); [restricted] unsigned long _stdcall AddRef(); [restricted] unsigned long _stdcall Release(); }
    5. COM Automatisierung und Clients
      • Automatisierung
      • Das Steuern von Anwendungen über COM-Interfaces wird als Automatisierung bezeichnet.
      • Clients
      • Im selben Prozess durch direkte Funktionsaufrufe
      • Auf derselben Maschine durch IPC calls
      Client Component Client Component COM Client Process Server Process
    6. Pywin32 Python for Windows extensions
      • Pywin32
      • Python-Unterstützung für Windows
      • Entwickelt von Mark Hammond
      • http://sourceforge.net/projects/pywin32/
      • win32com
      • Python COM Paket
      • Entwicklung von COM-Clients
      • Entwicklung vom COM-Servern
    7. „ Hello World“ Microsoft Word from win32com import client word = client.Dispatch( &quot;Word.Application&quot; ) word.Visible = 1 doc = word.Documents.Add() doc.Content.Text = &quot;Hello World&quot;
    8. Automatisierung von Applikationen
      • Ermittlung der ProgID
      • Methoden der Applikation
    9. Ermittlung der ProgID
      • Notwendig zum Ansprechen (“Dispatch”) der Applikation
      • Mit Hilfe von WBEM
      • Schnell , aber unvollständig
      • Skript: getProgIDsFromWBEM.py http://onyame.pastebin.ca/1522189
      • Ergebnis: progIDs_partialFromWBEM.txt
      • Über die Registry
      • Vollständig , aber sehr langsam
      • Skript: getProgIDsFromRegistry.py http://onyame.pastebin.ca/1522193
      • Ergebnis:
      from win32com.client import Dispatch excel = Dispatch( „Excel.Application&quot; )
    10. Beispiele für ProgIDs
      • Access.Application
      • Citrix.ICAClient
      • Excel.Application
      • MSProject.Application
      • PowerPoint.Application
      • Visio.Application
      • Word.Application
    11. Methoden der Applikation
      • COM Dokumentation der Applikationen
      • Manchmal schwer zu finden
      • Generierung eines Python-Moduls mit makepy
      • /Python25/Lib/site-packages/win32com/client/makepy.py
      • Vorteile im Editor (Eingebeerweiterung)
      • Etwas schneller
      • Browsen der Schnittstellen mit combrowse
      • /Python25/Lib/site-packages/win32com/client/combrowse.py
    12. Generierung eines Python-Moduls mit makepy
      • Beispiel PowerPoint
      • 91493440-5A91-11CF-8700-00AA0060263Bx0x2x8.py
    13. Generierung eines Python-Moduls mit makepy Beispiel-Ausschnitt: PowerPoint  Slides  Add class Slides(DispatchBaseClass): CLSID = IID( '{91493469-5A91-11CF-8700-00AA0060263B}' ) coclass_clsid = None # Result is of type Slide def Add(self, Index=defaultNamedNotOptArg, Layout=defaultNamedNotOptArg): ret = self._oleobj_.InvokeTypes(2004, LCID, 1 , ( 13 , 0 ), (( 3 , 1 ), ( 3 , 1 )),Index , Layout) if ret is not None : # See if this IUnknown is really an IDispatch try : ret = ret.QueryInterface(pythoncom.IID_IDispatch) except pythoncom.error: return ret ret = Dispatch(ret, 'Add' , '{91493445-5A91-11CF-8700-00AA0060263B}' , UnicodeToString=0) return ret
    14. Browsen der Schnittstellen mit combrowse
    15. Browsen der Schnittstellen mit combrowse PowerPoint
    16. Browsen der Schnittstellen mit combrowse Slides Add Slide Parameter
    17. Alternativer Browser Type Library Browser tlbrowse.py
    18. Beispiel from win32com import client ppt = client.Dispatch( „Powerpoint.Application&quot; ) ppt.Visible = 1 ppt.ActivePresentation.Slides.Add( 1 , 1 ) Index Layout
    19. Debugging
    20. Debugging Python Trace Collector
      • import win32traceutil
    21. Grundlegende Automatisierungen
      • Skripte
      • Events
      • Add-Ins
    22. Events
      • Event-Abarbeitung
      • Event-Handler definieren
      • Applikation mit Event-Handler anfordern („Dispatch“)
      • Event-Schleife: Auf Events warten
      • Mögliche Events
      • Window-Eigenschaften („Resize“ etc.)
      • Document-Eigenschaften („Zellen-Inhalte“ etc.)
      • Buttons (z.B. in Toolbars)
    23. Events Event-Handler definieren
      • Beispiel Excel
      • Beispiel PowerPoint
      class EventManager(object): def OnSheetBeforeDoubleClick(self, sheet, target, cancel): print „Do something“ # something pass class EventManager(object): def OnSlideShowNextSlide(self, Wn): print „Do something“ # something pass
    24. Events Welche Events gibt es? – Beispiel Excel
      • Aus 00020813-0000-0000-C000-000000000046.py
      OnGetTypeInfo OnWorkbookAddinInstall OnSheetBeforeRightClick OnSheetFollowHyperlink OnWorkbookBeforePrint OnWorkbookBeforeXmlExport OnWorkbookDeactivate OnSheetSelectionChange OnInvoke OnSheetActivate OnWorkbookAddinUninstall OnAddRef OnQueryInterface OnWorkbookSync OnSheetChange OnGetTypeInfoCount OnWorkbookBeforeSave OnWorkbookAfterXmlExport OnWorkbookPivotTableCloseConnection OnSheetCalculate OnWorkbookPivotTableOpenConnection OnSheetBeforeDoubleClick OnSheetDeactivate OnWindowDeactivate OnRelease OnWindowResize OnSheetPivotTableUpdate OnWorkbookOpen OnWorkbookBeforeXmlImport OnWindowActivate OnNewWorkbook OnWorkbookNewSheet OnWorkbookAfterXmlImport OnWorkbookBeforeClose OnGetIDsOfNames
    25. Events Welche Events gibt es? – Beispiel PowerPoint
      • Aus 91493440-5A91-11CF-8700-00AA0060263B.py
      OnPresentationBeforeSave OnColorSchemeChanged OnSlideShowNextSlide OnSlideShowNextClick OnPresentationOpen OnPresentationSave OnWindowBeforeRightClick OnSlideShowBegin OnWindowBeforeDoubleClick OnWindowSelectionChange OnNewPresentation OnWindowDeactivate OnPresentationClose OnAfterPresentationOpen OnAfterNewPresentation OnSlideShowNextBuild OnPresentationNewSlide OnPresentationSync OnSlideSelectionChanged OnWindowActivate OnSlideShowEnd OnPresentationPrint
    26. Events Applikation mit Event-Handler anfordern („Dispatch“) # Dispatch Excel with event handler from win32com.client import DispatchWithEvents excel = DispatchWithEvents( 'Excel.Application' , EventManager) excel.Visible = 1
    27. Events Event-Schleife: Auf Events warten # Listen for events import threading, pythoncom stopEvent = threading.Event() while True : pythoncom.PumpWaitingMessages() # Necessary so that python doesn't hog CPU stopEvent.wait( .2 ) if stopEvent.isSet(): stopEvent.clear() break
    28. Add-Ins
      • Erstellen von Add-Ins
      • Ermitteln einer CLSID für das Add-In
      • Cut & Paste der Add-In Klassendefinition
      • Überschreiben von Methoden der Add-In Klasse
      • Registrieren des Add-Ins
      import pywintypes clsid = pywintypes.CreateGuid()
    29. Add-Ins Klasse Addin class Addin(object): _com_interfaces_ = [ '_IDTExtensibility2' ] _public_methods_ = [] _reg_clsctx_ = pythoncom.CLSCTX_INPROC_SERVER _reg_clsid_ = '{2F1E606F-2A7B-46F9-AF6A-267C0036C348}' _reg_progid_ = 'Python.Test.Addin' _reg_policy_spec_ = 'win32com.server.policy.EventHandlerPolicy' def __init__( self ): import win32traceutil; self .application = None def OnConnection( self , application, connectMode, addin, custom): print 'OnConnection' , application, connectMode, addin, custom def OnDisconnection( self , mode, custom): print 'OnDisconnection ' ‚ mode, custom def OnAddInsUpdate( self , custom): print 'OnAddInsUpdate' , custom def OnStartupComplete( self , custom): print 'OnStartupComplete' , custom def OnBeginShutdown( self , custom): print 'OnBeginShutdown' , custom
    30. Demos
    31. Demo Twitter in PowerPoint
      • Twitter message on every PowerPoint OnSlideShowNextSlide event
      • The speaker note will be the message, if existing
      python-twitter
    32. Twitter in PowerPoint Implementation using Python’s win32com (1)
      • Event handler for PowerPoint
      import twitter api = twitter.Api(username= 'python_demo' , password= '*' ) class EventManager(object): def OnSlideShowNextSlide(self, Wn): i = powerpoint.ActivePresentation. SlideShowWindow.View.Slide.SlideIndex for shape in powerpoint.ActivePresentation. Slides[i-1].NotesPage.Shapes: if shape.TextFrame.HasText: notes = shape.TextFrame.TextRange.Text api.PostUpdate(notes)
    33. Twitter in PowerPoint Implementation using Python’s win32com (2)
      • Dispatch PowerPoint with event handler and listen for events
      from win32com.client import DispatchWithEvents powerpoint = DispatchWithEvents( 'PowerPoint.Application' , EventManager) powerpoint.Visible = 1 # Listen for events import threading, pythoncom stopEvent = threading.Event() while True : pythoncom.PumpWaitingMessages() stopEvent.wait( .2 ) if stopEvent.isSet(): stopEvent.clear() break Source: Roy Han’s PyCon 2008 tutorial “Automating Windows Applications with win32com” http://tr.im/q43o
    34. Quellen und Literatur
    35. Quellen
      • Tutorial Automating Windows Applications with win32com
      • Roy H. Han, Python Conference 2008, Chicago
      • http://us.pycon.org/2008/tutorials/AutomatingWindows/
      • Website Python for Windows – Ressources and examples
      • Mustafa Görmezer
      • http://win32com.goermezer.de/
    36. Literatur Python Programming on Win32
      • Mark Hammond, Andy Robinson: Python Programming on Win32

    + Andreas SchreiberAndreas Schreiber, 3 months ago

    custom

    402 views, 0 favs, 3 embeds more stats

    PyCologne-Vortrag (Köln, 12.08.2009)

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 402
      • 396 on SlideShare
      • 6 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 2
    Most viewed embeds
    • 3 views on http://wiki.python.de
    • 2 views on http://www.andreas-schreiber.net
    • 1 views on http://wp.andreas-schreiber.net

    more

    All embeds
    • 3 views on http://wiki.python.de
    • 2 views on http://www.andreas-schreiber.net
    • 1 views on http://wp.andreas-schreiber.net

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories