在Windows平台上,从原来使用C/C++编写原生EXE程序,到使用Python编写一些常用脚本程序,成熟的模块的使用使得编程效率大大提高了。

不过,python模块虽多,也不可能满足开发者的所有需求。而且,模块为了便于使用,通常都封装过度,有些功能无法灵活使用,必须直接调用Windows API来实现。

要完成这一目标,有两种办法,一种是使用C编写Python扩展模块,或者就是编写普通的DLL通过python的ctypes来调用,但是这样就部分牺牲掉了Python的快速开发、免编译特性。

还好,有一个模块pywin32可以解决这个问题,它直接包装了几乎所有的Windows API,可以方便地从Python直接调用,该模块另一大主要功能是通过Python进行COM编程。

该项目是开源的,项目地址是:GitHub - mhammond/pywin32: Python for Windows (pywin32) Extensions

api地址:

PyWin32 Documentation

安装时可以直接使用pip执行“pip install pywin32”来安装它。

安装完毕后,在Python安装路径下的Lib\site-packages\win32可以看到所有的API支撑模块,Lib\site-packages\win32com下则是COM的支撑模块。

python win32模块详解 python的win32模块用来干嘛_Windows

在Lib\site-packages下有一个PyWin32.CHM帮助文件,相信对Windows编程有一定基础的,看了这个帮助文件就能很快上手。

简单说,pywin32把Windows API按照功能分了一些大类,每一个大类作为一个模块。以下是所有的模块:

mmapfile:提供对内存映射文件 API 的访问

odbc:odb数据库api

perfmon:包装性能监视器功能的模块。

pywintypes:支持常见 Windows 类型的模块。

servicemanager:与 Windows 服务控制管理器交互的模块。

timer:包装 Win32 Timer 函数的扩展

win2kras:将 Windows 2000 扩展封装到远程访问服务 (RAS) API 的模块。

win32api:提供了常用的用户API

win32clipboard:提供了有关粘贴板的API

win32con:有关的常量

win32console:控制台

win32cred:凭证管理功能的接口。此模块中的功能仅适用于 Windows XP 及更高版本。

win32crypt:win32 Cryptography API 的接口

win32event:A module which provides an interface to the win32 event/wait API

win32evtlog:封装了 Windows Win32 事件日志 API。

win32file:提供了有关文件操作的API

win32gui:提供了有关windows用户界面图形操作的API

win32help:Win32 help API's

win32inet:An interface to the Windows internet (wininet) API

win32job:An interface to the win32 Process and Thread API's, available in Windows 2000 and later.

win32lz:封装 Windows LZ 压缩例程的模块

win32net:A module encapsulating the Windows Network API.

win32pdh:A module, encapsulating the Windows Performance Data Helpers API

win32pipe:管道api

win32print:A module encapsulating the Windows printing API.

win32process:进程api

win32profile:包装用于处理用户配置文件的函数

win32ras:A module encapsulating the Windows Remote Access Service (RAS) API.

win32security:An interface to the win32 security API's

win32service:服务

win32trace

win32transaction:Module wrapping Kernal Transaction Manager functions, as used with transacted NTFS and transacted registry functions.

win32ts:Interface to the Terminal Services Api All functions in this module accept keyword arguments

win32wnet:A module that exposes the Windows Networking API.

winxpgui:xp图像用户界面API

比如文件类API就在模块win32file中,进程类API在模块win32process中。

在使用的时候,按需导入相关模块就行了,win32con则定义了所有的常量,几乎是必不可少的,一些难以分类的API则在模块win32api中(大部分是kernel32.dll导出的API)。

部分模块之间还存在一些交叉,比如CreateFile的参数中用到的GENERIC_READ常量,在win32con中有定义,在win32file中也有定义。

用户只要大概知道这个是文件API用到的常量,那么不管你写win32file.GENERIC_READ还是win32con.GENERIC_READ都是可以的。

关闭句柄用的CloseHandle函数也是在两个模块中都有定义的。

需要注意的是,微软提供的Wsa系列网络API也都在win32file模块中,因为很多操作系统都是把套接字也用为文件对象来操作的。

如果你不清楚要使用的API在哪个模块中,那就到帮助文件里搜索一下,一定会给你答案的。

python win32模块详解 python的win32模块用来干嘛_python win32模块详解_02

如果你只是对pywin32中如何调用某个API不熟悉,那么查看Pywin32.CHM就足够了,如果你对API本身的参数定义和使用不熟悉,那还得继续看MSDN。

下面来写一个Helloworld作为开始吧:

import win32api
import win32con

win32api.MessageBox(None,"Hello,pywin32!","pywin32",win32con.MB_OK)

 效果如下:

python win32模块详解 python的win32模块用来干嘛_python win32模块详解_03

在Lib\site-packages\win32\Demos目录下有许多例子,如果你还不清楚pywin32怎么上手,来看看这些例子就知道了。

比如使用API进行文件操作:

# This is a "demo" of win32file - it used to be more a test case than a
# demo, so has been moved to the test directory.
# Please contribute your favourite simple little demo.
import win32file, win32api, win32con
import os

# A very simple demo - note that this does no more than you can do with
# builtin Python file objects, so for something as simple as this, you
# generally *should* use builtin Python objects.  Only use win32file etc
# when you need win32 specific features not available in Python.

def SimpleFileDemo():
    testName = os.path.join( win32api.GetTempPath(), "win32file_demo_test_file")
    if os.path.exists(testName): os.unlink(testName)
    # Open the file for writing.
    handle = win32file.CreateFile(testName,
                                  win32file.GENERIC_WRITE,
                                  0,
                                  None,
                                  win32con.CREATE_NEW,
                                  0,
                                  None)
    test_data = "Hello\0there".encode("ascii")
    win32file.WriteFile(handle, test_data)
    handle.Close()
    # Open it for reading.
    handle = win32file.CreateFile(testName, win32file.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
    rc, data = win32file.ReadFile(handle, 1024)
    handle.Close() #此处也可使用win32file.CloseHandle(handle)来关闭句柄
    if data == test_data:
        print "Successfully wrote and read a file"
    else:
        raise Exception("Got different data back???")

    os.unlink(testName)


if __name__=='__main__':
    SimpleFileDemo()

 可以看一看上面的例子是如何使用Windows API的,相信对于pywin32模块的使用会有一个初步的认识。

有了pywin32模块的辅助,对于Windows下的Python编程绝对是如虎添翼。