Printing from python on Windows platform

david.stegbauer at cz.opel.com david.stegbauer at cz.opel.com
Wed Aug 4 03:47:04 EDT 1999


Johann Spies write:
>In Linux I use the following to redirect printing to
>the printer:
>
>    drukker = sys.stdout
>    drukker = os.popen('lpr','w')
>    drukker.write(drukstuk)
>    sys.stdout.write(drukstuk)
>
>What would the windows equivalent be?
>
> I have tried os.popen('prn', 'w'), but without success.

Printing under windows is not such easy. I personally write to a file, then I
pass it to MS Word to print. I format the output using RTF, but I think there is
no problem to use HTML and Netscape - then it is sufficient to spawn it like

from os import spawnv, P_NOWAIT
spawnv(P_NOWAIT, r'D:\APP\NETSCAPE\PROGRAM\NETSCAPE.EXE', ('NETSCAPE.EXE',
'/print("filename.html")'))

David

8<---------------print-word.py--------------------

import struct
from time import sleep
from os import spawnv, P_NOWAIT
from dynwin import windll, windde

def PrintUsingWinWord(filename):
    app = r"C:\MSOFFICE\WINWORD\WINWORD.EXE"
    spawnv(P_NOWAIT, app, (app, "/n"))
    print 'spawned WinWord, sleeping 5 seconds'
    sleep(5)
    s = windde.dde_session()
    s.initialize()
    CC = windll.membuf(36)
    CC.write(struct.pack ('l',36) + (32 * '\000'))
    retries = 10

    while retries:
        try:
            s.connect('WinWord', 'System', CC)
            s.execute('[FileOpen("' + filename + '")]')
            #s.execute('[FilePrint 0][FileExit 2]')
            s.uninitialize()
            retries = 0
        except:
            print 'Unable to connect to WinWord, remain', retries, 'attempts'
            print 'sleeping 10 seconds'
            sleep(10)
            retries = retries - 1
    del s
    return






More information about the Python-list mailing list