Automating Microsoft Word

One small example how to automate Microsoft Word and printout a
postscript file with a specified postscript printer (yes, more than one
printers can be installed on the system).

Multithreadding:

Unlike
Powerpoint Microsoft Word can run in multiple instances – If a program
creates more Word processes you will see them in the task manager.

How to call the function:

You simply call the function with the name of the Word file, Postscript file and the printername which you want to use.:

eg.
make_ps.word(‘c:\test\wordfilename.doc’, ‘c:\test\psfilename.ps’, ‘My Postscript Printername’)

The source code:

import win32com.client, pythoncom, time

def word(wordfile, psfile, printer):
    pythoncom.CoInitializeEx(pythoncom.COINIT_APARTMENTTHREADED)
    myWord = win32com.client.DispatchEx('Word.Application')
    myWord.Application.ActivePrinter = printer
    myDoc = myWord.Documents.Open(wordfile, False, False, False)
    myDoc.Saved=1
    myWord.Application.NormalTemplate.Saved = 1
    myWord.PrintOut(True, False, 0, psfile)
    while myWord.BackgroundPrintingStatus > 0:
        time.sleep(0.1)
    myDoc.Close()
    myWord.Quit()
    del myDoc
    del myWord
    pythoncom.CoUninitialize()

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.