Using the Windows 32 API from Python

When running Python programs on a Windows computer there are often cases (such as better integration) where it makes sense to throw portability to the winds and call on the win32api for some platform-specific functionality.

  • 2010-Jul-27: How to get Windows Event Logs from within Python. [9330]
  • 2010-Jul-16: Some examples of using Python to drive Word and Excel via win32com. [9309]
  • 2010-Apr-30: PyWinAuto, automate Win32 GUI testing with Python scripts. This is now on PyPI here. Some recent news about this from the author. [136] [1]
  • 2010-Apr-22: registry is a wrapper around _winreg for working with the windows registry. An example of using _winreg directly. [7460]
  • 2010-Mar-31: This recipe provides a way to remotely shutdown Windows PCs from Python. And another approach to this task. This can also be done through the WMI module. And a short mention of using the win32api.InitiateSystemShutdown() function to do this. More discussion of ways to reboot can be found here. [8432]
  • 2010-Mar-08: Finding Installed Software using Python (on Windows of course). [9000]
  • 2010-Mar-08: How to find the commit charge values in Windows from Python. [8998]
  • 2010-Feb-10: How to lock the screen of a Windows box from within a Python script. [8968]
  • 2010-Feb-07: How to obtain More Windows System Information with Python, including the name of the workstation, logged in user (and his groups), IP and MAC addresses. [8954]
  • 2010-Feb-05: Scripting Photoshop with Python by using Photoshop's COM interface. [8949]
  • 2010-Jan-26: Using Python to create Windows shortcuts. [8935]
  • 2009-Nov-10: Talking to ActiveDirectory from Python and IronPython. [8726]
  • 2009-Oct-13: winguiauto.py (mentioned here) is a module to script driven Windows GUI testing. This has since been surpassed by things like watsup and pywinauto. [8627] [1]
  • 2009-Aug-04: How to shutdown a Windows PC using ctypes rather that the win32api package. [8361] [1]
  • 2009-Jul-31: A recipe for recursively querying the windows registry for subkeys. [8354]
  • 2009-Jul-16: An example of using the win32 package to collect information about what software is installed on a machine by reading the registry. [8282]
  • 2009-Jul-08: Running the Windows screen saver from Python. [8248] [1]
  • 2009-Jul-03: A short example of calling Python functions from a windows script by writing a COM server in Python. [8226]
  • 2009-Jun-19: python-excel.org is a site that is collecting information about working with Excel from Python. [8171]
  • 2009-May-04: regobj is another module for working with the Windows Registry. This one presents the registry as a set of keys from which you can navigate using dotted attributes. [7937]
  • 2009-Apr-09: jaraco.windows is a Python interface to the Windows APIs using ctypes. [7853]
  • 2009-Mar-13: xlrd, a package for extracting data from Excel spreadsheets. It is here on PyPI. [141]
  • 2009-Feb-25: To find the list of all the available drives on a Windows machine (with Python) you can do the following:
    import win32api
    s = win32api.GetLogicalDriveStrings().split("\x00")[:-1]
    The [:-1] is to drop the last element from the list which will be an empty string. This will give you a list of drive root directories like:
    ['C:\\', 'D:\\', 'G:\\', 'I:\\', 'S:\\', 'Y:\\', 'Z:\\']
    If you need to get the volume label, or some other information about each of these drives you can use the GetVolumeInformation function, like:
    t = win32api.GetVolumeInformation("C:\\")
    This returns a tupple like:
    ('MDATA', -590490522, 255, 6, 'FAT32')
    where the first element is the label of the drive (which can be handy if you have a few USB drives that Windows insists of giving different drive letters to depending on the order you insert them or power them up and you need to be able to find a particular drive to do some processing on). [7643]
  • 2009-Feb-13: Sending email on Python using the collaboration data objects (CDO) on Windows. [7580]
  • 2009-Jan-28: Controling CPU usage by using ctypes and Get CPU usage by using ctypes. [7499] [1]
  • 2009-Jan-16: Reading Outlook email with attachments from Python by using the COM interface. [7437]
  • 2008-Dec-14: Windows Management Instrumentation (WMI) in Python can be used to remotely monitor the health of Windows based computer. [143]
  • 2008-Nov-28: WATSUP is a Windows Application Test System Using Python. It can be used to start, stop, control and inspect a Windows application through a Python testing script allowing one to write unit and functional test sequences. This uses SendKeys for some of its functionality (note the examples need updating for Windows Vista). [7276] [1]
  • 2008-Nov-20: xlutils is a package for working with Excel files. [7240] [1]
  • 2008-Nov-11: Driving Visio from Python (and C# and F#) compared through some simple hello world examples. [7191]
  • 2008-Nov-03: python-ad is an Active Directory client library for Python. [7142]
  • 2008-Nov-03: Some notes on modifying the registry during an application install under Windows Vista. [7138]
  • 2008-Oct-08: This rant on the use of < pre>[6997]
  • 2008-Oct-03: A short script to modify the Windows registry to switch the Python command file associations between Python versions. And another script to search the registry. [6973]
  • 2008-Sep-23: How to monitor drives or directories for changes in files on a Windows platform. This can be used to quickly identify files that need to get backed up, or published or new drives being attached that should be scanned. This example looks at listening for USB device changes. This article talks abut this in general terms and the first comment corrects things and makes mention of ReadDirectoryChangesW() and the need for watcher threads to respond to WM_DEVICECHANGE notifications so that if the user wants to disconnect a USB drive the thread can terminate. An old posting: CD Insert Notification in WinXP goes into some detail about handling the WM_DEVICECHANGED message in Python. [6904]
  • 2008-Sep-19: comtypes, a pure Python COM package. More recently it is here on PyPI. The documentation is here. [137]
  • 2008-Sep-17: This article about Creating Excel spread sheets from Python programs discusses the simple approach of just writing a CSV formatted ASCII file as well as a more sophisticated approach using the pyExcelerator module to create a full XLS file. One could also use the win32com package to create XLS files by Office Automation techniques, but this requires that Excel is installed on the machine you run the Python script on. There is some more information on using pyExcelerator here. The xlwt module (a fork of the pyExcelerator project) can be user to create XLS spreadsheet files without requiring Excel being present. [5097] [1]
  • 2008-Sep-01: How to manipulate the read only (and other) file attribute from within Python. [6779]
  • 2008-Aug-26: This recipe demonstrates a simple windows service using the win32service module. Another recipe is here. [5306] [1]
  • 2008-Aug-20: Creating or updating the contents of Windows shortcut files. [6709] [1]
  • 2008-Aug-19: How to modify or set an environment variable permanently on Windows from within a Python script. [6703] [1]
  • 2008-Aug-06: An example of using the COM automation interface to control PhotoShop from Python. [6641]
  • 2008-Jul-18: How to list system process and process information on win32 [6545] [1]
  • 2008-Jul-14: A simple example of implementing a COM server in Python. [6519]
  • 2008-Jun-03: People do sneaky tricks in the Windows registry, such as hiding data behind NUL characters, which makes this data inaccessible though the usual regedit program. [6274]
  • 2008-May-02: Using the win32file module to quickly determine the size of directory trees. [6032] [1]
  • 2008-Apr-29: In Print Tkinter canvas without a postscript printer the author uses the win32ui module to create a device context on a Windows supported printer and then draws to it to get printed output. [5999] [1] [2]
  • 2008-Apr-25: Recreate MS Access table in SQLite, this has some limitations but could be used to help migrate data from one database to another. Also includes some use of the win32com API to get at the Access database. [5968] [1] [2]
  • 2008-Apr-19: PathCatcher is a Windows utility that allows one to right-click on a file or folder or selected group of files in Windows FileExplorer and save its path to the clipboard. Probably a good place to start if you want to write some simple extensions to the FileExplorer. [5933] [1]
  • 2008-Apr-03: Automate CATIA V5 with Python and Pywin32 is a simple example of the win32com.client module. [5385] [1]
  • 2008-Feb-27: A look at the various Python GUI programming platforms for Windows is worth a read, it gives you a brief code snippit for each along with a summary of their various strengths and weaknesses. [5175] [1]
  • 2008-Feb-25: A recipe for modifying the Windows registry that creates simple data files and feeds them to the "reg" command line tool. [5155] [1]
  • 2008-Jan-25: A set of Python Win32 howto articles from Tim Golden. This includes an example of how to check user credentials. [4993]
  • 2008-Jan-10: Making changes to the Windows Registry from Iron Python to support testing an application's call to the system web browser. [4642] [1]
  • 2008-Jan-07: Working with custom COM interfaces from Python, this talks about using the comtypes package to access COM objects with custom interfaces. [4605]
  • 2007-Nov-02: Using Win32 to schedule tasks from Python. [3918]
  • 2007-Sep-11: wmdlib is a library to interface with the Windows Media Devices API. [1880]
  • 2007-Aug-29: A class for writing content to Excel and formatting it, this recipe uses the win32com module to access and control Excel. [597] [1]
  • 2007-Aug-27: Convert Microsoft Office documents to PostScript (or PDF) using an installed printer driver and win32com. [584] [1] [2]
  • 2007-Aug-27: Using the win32com module to automate the CATIA CAD software package with Python. [583] [1]
  • 2007-Aug-25: VideoCapture, Win32 extension for working with video devices from Python [556]
  • 2007-Aug-23: Driving win32 GUIs with Python, a multi-part series that discusses how to send keystrokes and other events to a Windows GUI application [146]
  • 2007-Aug-23: pyADtools, a module for searching, creating and manipulating Active Directory objects [145]
  • 2007-Aug-23: Using IronPython with the WindowsForms API [144]
  • 2007-Aug-23: To get the Windows command shell to execute Python scripts by just typing in the name of a Python file (like script.py) and hitting return, you'll need to have the PATHEXT environment variable set up correctly. [142]
  • 2007-Aug-23: Python for .NET also exists. [140]
  • 2007-Aug-23: IronPython, is Python that can produce .NET bytecode and allows .NET libraries to be called from Python. Dynamically compiling C# from IronPython. [139]
  • 2007-Aug-23: DirectPython, a C++ extension to Python which provides access to the DirectX API including Direct3D, DirectSound, DirectShow and DirectInput. [138]
  • 2007-Aug-23: Simple Directmedia Layer has Python bindings, this is a cross-platform multimedia support library (its even used in the Linux port of "Civilization: Call to Power") [135]
  • 2007-Aug-23: Python Win32 Automation, automating the windows GUI with Python script. [134]
  • 2007-Aug-23: pyreg, a wrapper for Windows Registry functions [133]
  • 2007-Aug-23: comtypes, a pure Python COM package [132]
  • 2007-Aug-23: Running a Python script as a windows service using the srvany.exe is described here. However, using the Win32 extensions one can do the whole thing in Python, see Chapter 18 (pages 347-360) of  Python Programming on Win32 (see my recommendation of this book at the top of this page) for a good set of instructions and examples. [131]
  • 2007-Aug-23: Ming is a library to create Flash Shockwave files, it can also be used from Python, this site has examples [130]
  • 2007-Aug-23: pyreg, a module for working with the Windows Registry [129]
  • 2007-Aug-23: pyExcelerator: is a module for generating Excel97 files directly from Python [128] [1]
  • 2007-Aug-23: comtypes a new COM library for Python [127]
  • 2007-Aug-23: A tutorial on using COM with Python [126]
  • 2007-Aug-23: Documentation on the Win32 extensions for Python [125]

你可能感兴趣的:(Using the Windows 32 API from Python)