Python 3, bad output with repr()
Lele Gaifax <[email protected]>
| Newsgroups | gmane.comp.python.reportlab.user |
|---|---|
| Organization | Nautilus Entertainments |
| Message-ID | <[email protected]> |
Hi all, first of all, let me thank again Robin for his effort in porting RL to Py3! After latest announcement, I spent some time on the report side of my pet project which I ported to Py3. I was able to adapt my code and many outputs already work reasonably well (yeah!). With a few of them I have a strange result, where RL seems to apply repr() on plain strings and I fail to see why: it does not do that in other, more complex tables... so I cannot exclude I'm doing something wrong here. I'm attaching a stripped down version of one simple printout, as well as the PDF I'm getting: it's a "score card" for a game, where players fill in their scores, so it's basically just a "frame", without much text. Thanks in advance for any hint, ciao, lele. -- nickname: Lele Gaifax | Quando vivrò di quello che ho pensato ieri real: Emanuele Gaifas | comincerò ad aver paura di chi mi copia. [email protected] | -- Fortunato Depero, 1929.
p.py
(text/x-python, 5.7 KB)
from copy import copy
from reportlab.lib import colors
from reportlab.lib.enums import TA_CENTER
from reportlab.lib.pagesizes import A4
from reportlab.lib.units import cm
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import (BaseDocTemplate, Frame, FrameBreak,
KeepTogether,
PageTemplate, Paragraph, Spacer,
TableStyle)
from reportlab.platypus.tables import Table
base_style = getSampleStyleSheet()
cardinfo_style = copy(base_style['Italic'])
cardinfo_style.alignment = TA_CENTER
cardname_style = copy(cardinfo_style)
cardname_style.fontName = 'Times-BoldItalic'
cardname_style.fontSize = 12
cardname_style.leading = 13
cardlname_style = copy(cardname_style)
cardlname_style.fontSize = 9
cardlname_style.leading = 10
def gettext(x):
return x
class BasePrintout:
def __init__(self, output, columns):
self.output = output
self.columns = columns
def execute(self):
"Create and build the document."
self.createDocument()
self.doc.build(list(self.getElements()))
class ScoreCards(BasePrintout):
"Score cards, where match results are written by the competitors."
def __init__(self, output, columns=2):
super(ScoreCards, self).__init__(output, columns)
def createDocument(self):
from pkg_resources import get_distribution
title = gettext('Test title')
dist = get_distribution('sol')
description = dist.project_name
version = dist.version
doc = self.doc = BaseDocTemplate(
self.output, pagesize=A4, showBoundary=0,
leftMargin=0.5*cm, rightMargin=0.5*cm,
topMargin=0.5*cm, bottomMargin=0.5*cm,
author='%s %s' % (description, version),
subject=self.__class__.__name__,
title=title)
lp_frames = []
fwidth = doc.width / self.columns
fheight = doc.height
bmargin = doc.bottomMargin
for f in range(self.columns):
lmargin = doc.leftMargin + f*fwidth
lp_frames.append(Frame(lmargin, bmargin, fwidth, fheight))
templates = [PageTemplate(frames=lp_frames, onPage=self.decoratePage)]
doc.addPageTemplates(templates)
def decoratePage(self, canvas, doc):
"Add crop-marks to the page."
line = canvas.line
for iy in range(0, 4):
y = doc.bottomMargin + iy * (doc.height/3)
for ix in range(0, 3):
x = doc.leftMargin + ix * (doc.width/2)
line(x-5, y, x+5, y)
line(x, y-5, x, y+5)
def getElements(self):
boards = [(1, '', ''),
(2, '', '')]
data = [[gettext('Points'),
'',
gettext('Score'),
gettext('Coins'),
gettext('Queen'),
'',
'',
gettext('Coins'),
gettext('Score'),
'',
gettext('Points')]]
for i in range(9):
data.append(['', '', '', '', '', i+1, '', '', '', '', ''])
sw = self.doc.width/self.columns*0.95 / 23
ssw = sw/2
qw = sw*2
nw = sw*3
table_widths = (nw, ssw, nw, nw, qw, ssw, qw, nw, nw, ssw, nw)
table_style = TableStyle([('GRID', (0,1), (0,9), 1.0, colors.black),
('GRID', (-1,1), (-1,9), 1.0, colors.black),
('GRID', (2,1), (-3,9), 0.5, colors.black),
('ALIGN', (0,0), (-1,0), 'CENTER'),
('SIZE', (0,0), (-1,0), 8),
('ALIGN', (5,1), (5,-1), 'CENTER'),
('BACKGROUND', (5,1), (5,-2), colors.lightgrey),
('SIZE', (5,1), (5,-1), 8),
('SPAN', (4,0), (6,0)),
('ALIGN', (0,10), (-1,10), 'CENTER'),
('SIZE', (0,10), (-1,10), 8),
('BOX', (0,11), (0,11), 2.0, colors.black),
('BOX', (-1,11), (-1,11), 2.0, colors.black),
('ALIGN', (0,12), (-1,12), 'CENTER'),
('SIZE', (0,12), (-1,12), 8),
('BOX', (0,13), (0,13), 0.5, colors.black),
('BOX', (-1,13), (-1,13), 0.5, colors.black),
('SPAN', (1,10), (4,12)),
('SPAN', (6,10), (-2,12)),
('VALIGN', (0,10), (-1,13), 'MIDDLE'),
('SPAN', (1,-1), (-2,-1))
])
for i, board in enumerate(boards):
boardno = ['']*11
names = [[gettext('Final score'),
Paragraph(board[1],
len(board[1])<60 and cardname_style or cardlname_style),
'', '', '', '',
Paragraph(board[2],
len(board[2])<60 and cardname_style or cardlname_style),
'', '', '',
gettext('Final score')],
['']*11,
[gettext('Break')]+['']*9+[gettext('Break')],
boardno]
table = Table(data+names, table_widths, style=table_style)
if i == 0 or (i+1) % 3:
yield KeepTogether([table, Spacer(0, 0.6*cm)])
else:
yield table
yield FrameBreak()
if __name__ == '__main__':
maker = ScoreCards('/tmp/sc.pdf')
maker.execute()
sc.pdf
(application/pdf, 3.2 KB) - not displayed