Pretty report printing

Dale Strickland-Clark dale at out-think.NOSPAMco.uk
Fri Oct 27 15:41:21 EDT 2000


Dale Strickland-Clark <dale at out-think.NOSPAMco.uk> wrote:

>I need to produce a bunch of standard reports from a database and am
>considering Python.
>
>The reports need to be pretty but probably text only (although borders
>and boxes would be nice). They should be previewable online and
>printable to any Windows printer.
>
>I like the idea of using HTML but it doesn't understand the printed
>page so headings, headers and footers are trickey.
>
>Other options include PDF (which I'd have to research cos I know
>nothing about it) and driving Word or Excel through COM to build pages
>- which isn't an attractive thought.
>
>How have others tackled this problem?
>

I had this from Jim Dukarm by email which, while not applicable to my
requirement, did reveal an interesting feature of the % string
operator that I didn't know - and can't find in the docs. But then,  I
can never find any of the docs on the % string operator when I need
it!

Hi -
My news reader is fritzed at the moment, so I am just sending this to
you as a private e-mail. You are welcome to post it to
comp.lang.python yourself, if you wish.
There is a very simple way to print nice reports in Windows using a
Python app, as long as you have a word processor (such as MS Word)
which can read and write .rtf format. This trick is mainly applicable
in cases where you have a report with a fixed set of named (or
nameable) data items, rather than just an indefinite list of records.
First, use your word processor to create a report template which looks
like the final report in every respect except that the data items are
represented by tagged format expressions, as in this example:
Location:          %(location)s
Number of Widgets: %(numwidgets)02d

Save the template as an .rtf file.
In your Python app, create a Report class which reads the template out
of the file as a single text string:
f = open('template.rtf','r')
template_text = f.read()
f.close()

The class also saves the data items as attributes with names identical
to the corresponding format tags in the report template:
self.location = TheLocationFromTheDB
self.numwidgets = TheNumberOfWidgets

Then it substitutes the values into the template:
report_text = template_text % self.__dict__
Then it saves the report text as an .rtf file:
filename = 'widgetreport_%s.rtf' % self.customer_id f = open(filename,
'w') f.write(report_text) f.close()
At this point you can use your word processor to preview and print the
report. You can also archive the report files in case you might need
to run off another copy some day. Most of my Python apps that do this
sort of thing also drop a log entry into a tab-delimited file
indicating what document was generated when and for whom at the same
time that the report file is generated.
I use this technique extensively for my office recordkeeping and for
generating boilerplate e-mail notifications to my customers. It works
very well and uses up very few brain cells.
Jim Dukarm
DELTA-X RESEARCH


--
Dale Strickland-Clark
Out-Think Ltd
Business Technology Consultants





More information about the Python-list mailing list