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()