Printing in a generic way - like the rest of Python

John Hall wweexxsseessssaa at telusplanet.net
Sun May 18 22:05:13 EDT 2003


On Mon, 12 May 2003 14:13:13 GMT, John Hall
<wweexxsseessssaa at telusplanet.net> wrote:

>I'm thinking of building an *.rtf file, which _IS_ plain text but
>displays/prints fancy fonts, colours, graphics etc in an RTF "reader",
>e.g. MS Windows WordPad....

(Need I apologise for quoting myself?)

For completeness, here is my (reworked sample) template file, and the
code which drives it. Short enough to post here, I reckon.

<sample_t.rtf>
{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fswiss\fprq2\fcharset0
Copperplate Gothic Bold;}{\f1\fswiss\fcharset0
Arial;}{\f2\froman\fprq2\fcharset0 Times New Roman;}}
{\colortbl ;\red0\green0\blue255;\red0\green128\blue128;}
\viewkind4\uc1\pard\cf1\ul\b\f0\fs96 Guest Pickup\cf2\par
\cf1\ulnone\b0\fs32\par
Pickup information:\cf0\f1\fs20\par
\f2\fs24 $$$Pickup$$$\par
\cf1\b\par
\f0\fs32 Guest names:\cf0\b0\f1\fs20\par
\f2\fs24 $$$Names$$$\par
\par
\cf1\f0\fs32 Notes:\cf0\f1\fs20\par
\f2\fs24 $$$Notes$$$\par
\par
}

#
## Generate & print a Pickup sheet
#
    def on_btnPickupSheets_mouseClick(self, event):
        # collect the variable information. 
        #  FAKED DURING DEVELOPMENT!!!
        pickup="Saturday, May 31. 13:30 Airport, UA497"
        names=["Jack Smith","Julie Smith","Andrew Smith"]
        notes="""There may be newline characters embedded in notes,
so we'll need to be careful
about handling those"""
        
        template=open("sample_t.rtf", "r") # open the template 
        printdoc=open("sample_p.rtf", "w") # open the target        
        # copy template lines into rtf document, 
        #  replacing keywords with variable text
        for line in template.xreadlines():
            # handle pickup info (a single line)
            if line.find("$$$Pickup$$$")>-1: # keyword in Template
                parts=line.split("$$$") # split into pre & post
                printdoc.write(parts[0]+pickup+parts[2])
            
            # handle names (a list of single lines)
            elif line.find("$$$Names$$$")>-1:
                parts=line.split("$$$")
                for n in range(0,len(names)):
                    if n<len(names)-1: # NOT the last name,
                       # DON'T tweak fonts etc
                        printdoc.write(parts[0]+names[n]+"\\par\n")
                    else:    # IS the last name
                        printdoc.write(parts[0]+names[n]+parts[2])
                             # tweak fonts etc
             
            # handle notes (which may contain newlines)
            elif line.find("$$$Notes$$$")>-1:
                parts=line.split("$$$")
                notes=notes.replace("\n","\\par\n")
                printdoc.write(parts[0]+notes+parts[2])
            else:
                printdoc.write(line) 
                  # Just copy any "un-interesting" lines
        template.close()
        printdoc.close()
        
        # in Windows, print it via Wordpad.
        whichwin=os.environ["windir"]
        if whichwin.find("WINNT")>-1:
            os.system('"C:\Program Files\Windows
[cont]NT\Accessories\wordpad.exe" /p sample_p.rtf')
        elif (whichwin.find("WINDOWS")>-1) or
[cont](whichwin.find("WIN98")>-1):
            os.system('"C:\Program Files\Accessories\wordpad.exe" /p
[cont]sample_p.rtf')
         # Linux??? Document tweaking should be the same 
         #  (but watch out with newlines)
         #   Printing it will require some investigation etc.

-- 
John W Hall <wweexxsseessssaa at telusplanet.net>
Calgary, Alberta, Canada.
"Helping People Prosper in the Information Age"




More information about the Python-list mailing list