Skip to Main Content
Python Programming On Win32
book

Python Programming On Win32

by Andy Robinson, Mark Hammond
January 2000
Intermediate to advanced content levelIntermediate to advanced
672 pages
21h 46m
English
O'Reilly Media, Inc.
Content preview from Python Programming On Win32

A Simple Example

Let’s start with the simplest possible example:

from win32com.client import Dispatch
MYDIR = 'c:\\data\\project\\oreilly\\examples\\ch12_print'

def simple():
    myWord = Dispatch('Word.Application')
    myWord.Visible = 1  # comment out for production
    
    myDoc = myWord.Documents.Add()
    myRange = myDoc.Range(0,0)
    myRange.InsertBefore('Hello from Python!')
    
    # uncomment these for a full script
    #myDoc.SaveAs(MYDIR + '\\python01.doc')
    #myDoc.PrintOut()
    #myDoc.Close()

When you execute this function, Word starts, and your message is displayed. We’ve commented out the lines at the bottom, but you can choose to print, save, or close the document. It’s fun to arrange the Python console and Word side-by-side and watch your text appearing, and a great way to learn the Word object model.

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Qt5 Python GUI Programming Cookbook

Qt5 Python GUI Programming Cookbook

B.M. Harwani

Publisher Resources

ISBN: 1565926218Supplemental ContentErrata Page