[python-win32] Printing with COM triggered by a python cgi script
Douglas F. Schieszer
doug.schieszer at sprintsvslabs.com
Fri Jan 23 16:25:55 EST 2004
I am trying to learn how to create a python cgi web application. I have
created a module that I want to use to print a reciept using the python
com interface that is based on the "Interfacing with COM" example in
learning python 1st edition. I will include it below. This
print_testdrive() function works fine when called from a command line with
the proper objects as arguments, but when I execute it as part of a cgi
script it gives no errors but does not print anything. Any ideas what I
have done wrong?
reciept.py:
from win32com.client import constants, Dispatch
WORD = 'Word.Application'
False, True = 0, -1
import string
class Word:
def __init__(self):
self.app = Dispatch(WORD)
def open(self, doc):
self.app.Documents.Open(FileName=doc)
def replace(self, source, target):
self.app.Selection.HomeKey(Unit=constants.wdLine)
find = self.app.Selection.Find
find.Text = "%" + source + "%"
self.app.Selection.Find.Execute()
self.app.Selection.TypeText(Text=target)
def printdoc(self):
self.app.Application.PrintOut()
def close(self):
self.app.ActiveDocument.Close(SaveChanges=False)
def print_testdrive(customer, vehicle, appointment):
word = Word()
word.open(r"c:\Program Files\Apache
Group\Apache2\htdocs\templates\testdrive.doc")
if customer.nameprefix:
word.replace("nameprefix",customer.nameprefix)
else:
word.replace("nameprefix", " ")
if customer.firstname:
word.replace("firstname",customer.firstname)
else:
word.replace("firstname", " ")
if customer.middlename:
word.replace("middlename",customer.middlename)
else:
word.replace("middlename", " ")
if customer.lastname:
word.replace("lastname",customer.lastname)
else:
word.replace("lastname", " ")
if customer.namesuffix:
word.replace("namesuffix",customer.namesuffix)
else:
word.replace("namesuffix", " ")
word.replace("time",appointment.time)
word.replace("date",appointment.date)
word.replace("reservation",appointment.reservation)
word.replace("VIN",vehicle.VIN)
word.replace("Make",vehicle.Make)
word.replace("Model",vehicle.Model)
word.replace("Color",vehicle.Color)
word.replace("OptionPackage",vehicle.OptionPackage)
word.replace("Accessories",vehicle.Accessories)
word.replace("ModelYear",vehicle.ModelYear)
word.printdoc()
word.close()
More information about the Python-win32
mailing list