
Publishing ODBC database content as PDF.
This is an example from the upcoming next release of my
PDF conversion toolkit, xtopdf:.
You will need
Python v2.2 or greater, the
open source version of the ReportLab toolkit v1.17 or greater, and
xtopdf v1.0.
Follow the installation instructions for each package below.
First install Python.
Then install ReportLab, in a directory on your
PYTHONPATH.
Then install xtopdf in some directory which is on, or which you must add to, your PYTHONPATH.
Then copy the program below to some directory and run it with Python.
[The code below is only for demo purposes.]
#----------------------------- TestPyODBC01.py ------------------------
# TestPyODBC01.py
# Copyright (C) 2006, Vasudev Ram -
http://www.dancingbison.com# Released under the BSD license.
# *** This is alpha code, for demo purposes only. USE AT YOUR OWN RISK ***
import sys
import string
import odbc
# add path for import of xtopdf modules/classes to Python system path
# Change the line below to point to the folder where you extract the xtopdf .tgz file.
sys.path.append(r"C:\Software\xtopdf\xto
pdf-1.0")
from PDFWriter import PDFWriter
# You need to create an ODBC DSN (Data Source Name) with the name in the line below.
conn = odbc.odbc("PyODBCTest01")
# For this example I created a DBF database and file, to which the DSN points, but
# the code should work with any ODBC database.
print conn
#print type(conn)
curs = conn.cursor()
print curs
#print type(curs)
#rc = curs.execute("create table contact (fname char(5))")
#print "curs.execute create table rc = ", rc
#rc = curs.execute(" insert into contact values('AAAAA') ")
#print "curs.execute insert into contact rc = ", rc
#for i in range(1000):
# str_i = string.zfill(str(i), 5)
# rc = curs.execute(" insert into contact values(" + str_i + ") ")
# print "curs.execute insert into contact rc = ", rc
# Create a PDFWriter object
pw = PDFWriter('TestPyODBC01.pdf')
# Set its font
pw.setFont("Courier", 10)
# Set its header and footer
pw.setHeader("TesTPyODBC01.py: CONTACT.DBF - Header")
pw.setFooter("TesTPyODBC01.py: CONTACT.DBF - Footer")
# Execute the select query
rc = curs.execute(" select * from contact ")
print "curs.execute select * from contact rc = ", rc
# Fetch the data
a_tuple = ()
data = ""
#lin_ctr = 0
while 1:
# while data is not None:
data = curs.fetchone()
if data is None or type(data) != type(a_tuple):
break
#print "type(data) = ", type(data)
#print "curs.fetchone data = ", data
#lin_ctr += 1
out_str = string.zfill(data[0], 5)
pw.writeLine(out_str)
# page break every 66 lines
#if (lin_ctr == 66):
# pw.savePage()
# lin_ctr = 0
curs.close()
pw.savePage()
pw.close()
print "Done."
#----------------------------- EOF: TestPyODBC01.py ------------------------