pywin32访问word文件

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import win32com.client as win32
from time import sleep

RANGE = range(3, 8)

def word():
    word = win32.gencache.EnsureDispatch('Word.Application')
    doc = word.Documents.Add()
    word.Visible = True
    sleep(1)

    rng = doc.Range(0,0)
    rng.InsertAfter('Hacking Word with Pythonrnrn')
    sleep(1)
    for i in RANGE:
        rng.InsertAfter('Line %drn' % i)
        sleep(1)
    rng.InsertAfter("rnPython rules!rn")

    doc.Close(False)
    word.Application.Quit()

if __name__ == '__main__':
    word()

pywin32访问excel

参考链接

直接用上面的代码吧

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
import time
import win32com.client as win32
 
#----------------------------------------------------------------------
def excel():
    """"""
    xl = win32.gencache.EnsureDispatch('Excel.Application')
    ss = xl.Workbooks.Add()
    sh = ss.ActiveSheet
 
    xl.Visible = True
    time.sleep(1)
 
    sh.Cells(1,1).Value = 'Hacking Excel with Python Demo'
 
    time.sleep(1)
    for i in range(2,8):
        sh.Cells(i,1).Value = 'Line %i' % i
        time.sleep(1)
 
    ss.Close(False)
    xl.Application.Quit()
 
if __name__ == "__main__":
    excel()

pywin32访问access数据

pywin32真强大,可以通过com接口做你想做的事。比如操作excel, access等等。下面是访问access数据库的例子:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
import os
import win32com.client

conn = win32com.client.Dispatch("ADODB.Connection")

# Either way works: one is the Jet OLEDB driver, the other is the
# Access ODBC driver.  OLEDB is probably better.

db = r"c:test.mdb"
DSN="Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + db
#DSN="Driver={Microsoft Access Driver (*.mdb)};DBQ=" + db
conn.Open(DSN)

rs = win32com.client.Dispatch("ADODB.Recordset")
rs.Open( "[test]", conn, 1, 3 )

print rs.Fields.Count, " fields found:"
for x in range(rs.Fields.Count):
    print rs.Fields.Item(x).Name,
    print rs.Fields.Item(x).Type,
    print rs.Fields.Item(x).DefinedSize,
    print rs.Fields.Item(x).Value

在windows下通过pythoncom获取快捷方式所指定的路径

直接代码:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import pythoncom

def GetURLFromShortcut(url):
    shortcut = pythoncom.CoCreateInstance(
                                shell.CLSID_ShellLink, None,
        			pythoncom.CLSCTX_INPROC_SERVER, shell.IID_IShellLink)
    shortcut.QueryInterface( pythoncom.IID_IPersistFile ).Load(url)
    url = shortcut.GetPath(shell.SLGP_SHORTPATH)[0]

    return url

python获取PE文件版本信息

通过pywin32中的win32api.GetFileVersionInfo来获取

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Get the version information from Windows PE

from win32api import GetFileVersionInfo, LOWORD, HIWORD

def get_version_number (filename):
    try:
        info = GetFileVersionInfo (filename, "\")
        ms = info['FileVersionMS']
        ls = info['FileVersionLS']
        return HIWORD (ms), LOWORD (ms), HIWORD (ls), LOWORD (ls)
    except:
        return 0,0,0,0

def printOtherInfo(filename):
    try:
        info = GetFileVersionInfo(filename, "\")
        print(info)

	# In order to get the company information, we need to get the lang 
	# and code page first, then get the related strings
        trans = GetFileVersionInfo(filename, "\VarFileInfo\Translation")
        if not trans:
            return 
        print(info)

        # Common string lists:
        # "CompanyName","FileDescription", "FileVersion", "InternalName",
        # "LegalCopyright", "OriginalFilename", "ProductName", "ProductVersion"
        # You can use this way to get custom defined strings.

        info = GetFileVersionInfo(filename, "\StringFileInfo\%04x%04x\%s" % (trans[0][0], trans[0][1], "CompanyName"))
        print(info)
        info = GetFileVersionInfo(filename, "\StringFileInfo\%04x%04x\%s" % (trans[0][0], trans[0][1], "FileVersion"))
        print(info)
    except:
        pass

if __name__ == '__main__':
    import os
    filename = os.environ["COMSPEC"]
    print ".".join ([str (i) for i in get_version_number (filename)])
    printOtherInfo(filename)