October 24, 2006

Printing Word And PDF files from Python

Posted in Software at 20:13 by graham

Recently I had to automate printing a whole bunch of CVs on Windows. Having successfully avoided VBA my whole programming life, it was time to think fast. Thankfully Python has some win32com bindings, which allows you to talk COM to various Windows applications, and get them to print the documents.

First you need the Python win32com bindings.

Printing Word documents

Microsoft Word has a nice COM interface, which is well documented. The documentation is targetted at VBA, but the methods are the same.

    from win32com import client
    import time

    word = client.Dispatch("Word.Application")

    def printWordDocument(filename):

        word.Documents.Open(filename)
        word.ActiveDocument.PrintOut()
        time.sleep(2)
        word.ActiveDocument.Close()

    word.Quit()

This opens the Word application without making it visible to the user, opens the document, prints it out on the users default printer, and closes the document, then the application.

The only catch is if the printing throws up a dialog box (something like the document extends out of the print margins), this is displayed to the user. You might want to extend the time.sleep(2) to 5 seconds, to give the user time to click OK before you close the document.

Printing PDF documents

Adobe Acrobat doesn’t have a COM interface. For that you have to buy Acrobat Writer. Thankfully Internet Explorer comes to the rescue (I never thought I would say that). IE has a COM interface, and via it you can control the embedded Acrobat.

    from win32com import client
    import time

    ie = client.Dispatch("InternetExplorer.Application")

    def printPDFDocument(filename):

        ie.Navigate(filename)

        if ie.Busy:
            time.sleep(1)

        ie.Document.printAll()
        time.sleep(2)

    ie.Quit()

The only catch is you need at least Acrobat 7.0.7. Versions prior to 7 don’t have the printAll method, and 7 versions prior to 7.0.7 have an bug. The first time your script tries to print this dialog box appears:

WARNING! A script has requested to print an Acrobat file. This could print an entire document. Do you want to proceed printing?

Click the Don't ask me again tick box and say Yes. On versions prior to 7.0.7 it always remembers No, even when you said Yes. Further details on the bug, and how I found out about it, are here.

You can find documentation on the IE COM interface at MSDN.

What about from Java

I have used JACOB to call COM objects from Java on Windows before, and it worked well. The application names and methods will be the same in any language.

5 Comments »

  1. [ubuntu] Docuprint CP105b | Coders & Admins said,

    March 22, 2012 at 10:44

    […] Graham King’s blog has some code to print in windows here: http://www.darkcoding.net/software/p…s-from-python/ […]

  2. przemeq said,

    October 4, 2009 at 16:57

    Hello tuco,

    I had just the same problem with Acrobat 9.0.x. After you do ie.Navigate(”test_file.pdf”) do also ie.Visible = 1 and check out the URL on the address toolbar ;)

    in my case the URL was wrong, what IE expects is something like: c:\Documents and Settings\User01\My Documents\test_file.pdf

    Good luck!

  3. Yuppe said,

    February 7, 2008 at 22:25

    At http://win32com.goermezer.de/content/view/37/187/ is a related example which directly prints the page to a printer without prompting.

    Mustafa http://www.goermezer.de

    [graham] Thanks Mustafa!

  4. tuco said,

    January 29, 2008 at 20:48

    Here is a solution I found on the net for PDF files. You need to know the printer name as Windows wants to see it. In my case, it was a network printer generically shown here.

    import win32api import win32print

    win32print.SetDefaultPrinter(“\\server\share”) win32api.ShellExecute(0, “print”, “test_file.pdf”, “.”, 0)

  5. tuco said,

    January 29, 2008 at 19:27

    Acrobat v7.0.9 Python v2.5 POS IE6

    >>> from win32com import client >>> ie = client.Dispatch(“InternetExplorer.Application”) >>> ie.Navigate(“test_file.pdf”) >>> ie.Document.printAll() Traceback (most recent call last): File “(stdin)”, line 1, in file “C:\Python25\lib\site-packages\win32com\client\dynamic.py”. line 496, in getattr raise AttributeError, “%s.%s” % (self.username, attr) AttributeError: Document.printAll

Leave a Comment

Note: Your comment will only appear on the site once I approve it manually. This can take a day or two. Thanks for taking the time to comment.