1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
| # dumbTable.py
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import landscape, A4
from reportlab.lib.units import cm
#################################################
# data
## dList = ["Toto%s"%i for i in range(5000)]
dList = ["Toto%s"%i if (i%1000 != 0) else "Oulalalalala%s"%i for i in range(5000)]
print "dList:", len(dList), dList
fontName = "Helvetica"
fontSize = 8
print "fontName, fontSize:", fontName, fontSize
filename = "dumbTable.pdf"
print "filename:", filename
title = "dumbTable"
widthMargin = 0.5
heightMargin = 0.5
#################################################
# en attendant de trouver mieux
def getHfromFont(fontName, fontSize):
""
return (1.2*fontSize)
#
def getColFromList(dList, c, maxWidth, maxHeight, fontName, fontSize):
"retourne la colonne a imprimmer et sa largeur. si la colonne est vide, c'est que ca ne rentre pas !!!"
#
colList = []
#
mw = 0
currentH = 0
while (len(dList) > 0):
d = dList[0]
w = c.stringWidth(d, fontName=fontName, fontSize=fontSize)
h = getHfromFont(fontName, fontSize)
if ((currentH + h < maxHeight) and (w < maxWidth)): # ok, add to col
mw = max(mw, w)
currentH += h
colList.append(dList.pop(0))
else: # KO, col is full(height) or will not fit(width)
if (w >= maxWidth): # col will not fit in
while (len(colList) > 0): # put them back in dList
dList.insert(0, colList.pop())
break
#
return colList, mw
#################################################
# page
width, height = landscape(A4)
print "width, height:", width, height
#################################################
# canvas
myCanvas = canvas.Canvas(filename, pagesize=landscape(A4))
myCanvas.setTitle(title)
myCanvas.setFont(fontName, fontSize)
#################################################
# try 1
xinit = widthMargin*cm
yinit = height - heightMargin*cm - getHfromFont(fontName, fontSize)
maxHeight = height - heightMargin*cm - heightMargin*cm
x = xinit
while (len(dList) > 0):
myCanvas.setFont(fontName, fontSize)
maxWidth = max(0, width - widthMargin*cm - x)
print "maxWidth:", maxWidth
y = yinit
colList, mw = getColFromList(dList, myCanvas, maxWidth, maxHeight, fontName, fontSize)
print "len(colList), mw:", len(colList), mw
if (len(colList) > 0): # col fit on the same page
# needs a new column on the same page
print "-! needs a new COLUMN on the same page"
for d in colList: # write column
myCanvas.drawString(x, y, d)
y -= getHfromFont(fontName, fontSize)
if (x > xinit): # not first column => draw line
myCanvas.rect(x - xinit/2, height - yinit - getHfromFont(fontName, fontSize), 0.25, maxHeight, fill=1)
x += mw + xinit
else: # col does not fit on the same page
if (x == xinit): # KO, it will never fit :-(
print "!!! error, it will never fit :-("
break
# needs a new page for the next column
print "-! needs a new PAGE for the next column"
myCanvas.showPage()
x = xinit
#
myCanvas.save() |
Partager