142 lines
6 KiB
Python
142 lines
6 KiB
Python
#!/usr/bin/python
|
|
# -*- coding: utf-8 -*-
|
|
|
|
from reportlab.lib.pagesizes import A4, landscape, A3, portrait
|
|
from reportlab.platypus import BaseDocTemplate, Frame, PageTemplate
|
|
from reportlab.lib.units import mm
|
|
from reportlab.platypus.flowables import PageBreak, Spacer
|
|
from reportlab.platypus.tables import Table
|
|
from reportlab.platypus.paragraph import Paragraph
|
|
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
|
from reportlab.lib import colors
|
|
|
|
|
|
"""
|
|
def make_portrait(canvas,doc):
|
|
canvas.setPageSize(PAGE_SIZE)
|
|
|
|
def make_landscape(canvas,doc):
|
|
canvas.setPageSize(landscape(PAGE_SIZE))
|
|
"""
|
|
|
|
|
|
class BaseStyle:
|
|
BASE = (
|
|
('GRID', (0, 0), (-1, -1), 0.7, colors.grey),
|
|
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
|
('FONTSIZE', (0, 0), (-1, -1), 7),
|
|
('FONTSIZE', (1, 0), (1, 0), 9),
|
|
('FONTSIZE', (4, 0), (-1, -1), 6),
|
|
('FONTSIZE', (2, 1), (2, -1), 4),
|
|
# ('VALIGN', (2, 1), (2, -1), 'MIDDLE'),
|
|
('BOX', (0, 0), (-1, -1), 1, colors.black),
|
|
('BACKGROUND', (0, 1), (-1, 1), (0, 1.0, 1.0)),
|
|
('BACKGROUND', (0, 2), (-1, 2), (0.4, 1.0, 1.0)),
|
|
('BACKGROUND', (0, 3), (-1, 3), (0.7, 1.0, 1.0)),
|
|
('ALIGN', (3, 0), (-1, -1), 'CENTER'),
|
|
('ALIGN', (0, 0), (0, -1), 'CENTER'),
|
|
('ROWBACKGROUNDS', (0, 4), (-1, -1), (colors.white, colors.lightgrey))
|
|
)
|
|
|
|
|
|
class TrophyPdf:
|
|
"""To build a pdf report from a series of tables"""
|
|
def __init__(self, filename='default_name.pdf', separated_pages=False, page='A4P'):
|
|
"""page can be A4P, A4L, A3P, A3L"""
|
|
self.separated_pages = separated_pages
|
|
self.styleSheet = getSampleStyleSheet()
|
|
self.MARGIN_SIZE = 10 * mm
|
|
if page == 'A4L':
|
|
self.PAGE_SIZE = landscape(A4)
|
|
elif page == 'A4P':
|
|
self.PAGE_SIZE = portrait(A4)
|
|
elif page == 'A3L':
|
|
self.PAGE_SIZE = landscape(A3)
|
|
elif page == 'A3P':
|
|
self.PAGE_SIZE = portrait(A3)
|
|
else:
|
|
raise ValueError(f'page value not recognized: {page}. Possible values are: A4P, A4L, A3P, A3L')
|
|
|
|
self.filename = filename
|
|
self.story = []
|
|
self.style = list(BaseStyle.BASE)
|
|
|
|
def commit(self):
|
|
self.__create_pdfdoc(self.filename, self.story)
|
|
|
|
def __create_pdfdoc(self, pdfdoc, story):
|
|
"""Creates PDF doc from story."""
|
|
pdf_doc = BaseDocTemplate(pdfdoc, pagesize=self.PAGE_SIZE,
|
|
leftMargin=self.MARGIN_SIZE, rightMargin=self.MARGIN_SIZE,
|
|
topMargin=self.MARGIN_SIZE, bottomMargin=self.MARGIN_SIZE)
|
|
main_frame = Frame(self.MARGIN_SIZE, self.MARGIN_SIZE,
|
|
self.PAGE_SIZE[0] - 2 * self.MARGIN_SIZE, self.PAGE_SIZE[1] - 2 * self.MARGIN_SIZE,
|
|
leftPadding=0, rightPadding=0, bottomPadding=0,
|
|
topPadding=0, id='main_frame')
|
|
main_template = PageTemplate(id='main_template', frames=[main_frame])
|
|
pdf_doc.addPageTemplates([main_template])
|
|
pdf_doc.build(story)
|
|
|
|
def add_table(self, table=None, title=None):
|
|
if table is None:
|
|
return
|
|
if self.separated_pages and (0 != len(self.story)):
|
|
self.story.append(PageBreak())
|
|
if title is not None:
|
|
self.story.append(Paragraph(title, self.styleSheet['BodyText']))
|
|
self.story.append(Spacer(0, 4 * mm))
|
|
if table is not None:
|
|
nrows = len(table)
|
|
heights = [9*mm, ]
|
|
heights.extend((5*mm,)*(nrows-1))
|
|
self.style = list(BaseStyle.BASE)
|
|
y = 0
|
|
for row in table:
|
|
x = 0
|
|
for el in row:
|
|
if el[0] == '_' and el[-1] == '_':
|
|
self.style.append(('BACKGROUND', (x, y), (x, y), colors.pink))
|
|
x += 1
|
|
y += 1
|
|
self.story.append(Table(data=table, style=self.style, rowHeights=heights))
|
|
|
|
|
|
if __name__ == '__main__':
|
|
tb = (
|
|
[['Rank', 'Atleta (M18)', 'Soc.', 'Punteggio\nNetto', 'Punteggio\nLordo', 'TROFEO EMI\n2017-02-26',
|
|
'13^ Ori de\n2017-03-19', '1 Maggio 2\n2017-05-01', 'Trofeo Emi\n2017-05-27', 'Trofeo ER \n2017-05-28',
|
|
'Campionato\n2017-06-18', 'Camp. Reg.\n2017-06-25', 'Scaffaiolo\n2017-07-16', 'Campionato\n2017-08-27',
|
|
'Trofeo Emi\n2017-09-30', 'Trofeo Emi\n2017-10-01', 'Monte Mori\n2017-10-08'],
|
|
['1', 'MANNOCCI Daniele', "0221\nPol 'G. MASI' ", ' 652.91', ' 652.91', ' ', '100.00', ' 87.38', '100.00',
|
|
'100.00', ' ', '100.00', '100.00', ' 65.53', ' ', ' ', ' '],
|
|
['2', 'VESCHI Francesco', '0610\nOr. CLUB APPENNIN', ' 383.27', ' 383.27', ' 51.87', ' ', ' 61.29', ' 61.05',
|
|
' 73.97', ' ', ' ', ' ', ' 37.81', ' 97.28', ' ', ' '],
|
|
['3', 'MIRZA Lucian', '0098\nAtl. INTERFLUMINA', ' 366.51', ' 366.51', ' 96.10', ' ', '100.00', ' ', ' ', ' ',
|
|
' ', ' ', ' ', ' ', ' 90.08', ' 80.32'],
|
|
['4', 'RONTINI Mattia', '0255\nASDS CARCHIDIO-ST', ' 355.28', ' 355.28', ' 62.64', ' 88.11', ' 88.70', ' ',
|
|
' 81.60', ' ', ' ', ' ', ' 34.22', ' ', ' ', ' ']]
|
|
|
|
)
|
|
|
|
mstyle = [
|
|
('GRID', (0, 0), (-1, -1), 0.7, colors.grey),
|
|
('FONTSIZE', (0, 0), (-1, -1), 6),
|
|
('FONTSIZE', (4, 0), (-1, -1), 5),
|
|
('FONTSIZE', (2, 1), (2, -1), 5),
|
|
|
|
# ('GRID',(1,1),(-2,-2),1,colors.green),
|
|
# ('BOX',(0,0),(1,-1),2,colors.red),
|
|
('BOX', (0, 0), (-1, -1), 1, colors.black),
|
|
# ('LINEABOVE',(1,2),(-2,2),1,colors.blue),
|
|
# ('LINEBEFORE',(2,1),(2,-2),1,colors.pink),
|
|
# ('BACKGROUND', (0, 0), (0, 1), colors.pink),
|
|
# ('BACKGROUND', (1, 1), (1, 2), colors.lavender),
|
|
# ('BACKGROUND', (2, 2), (2, 3), colors.orange),
|
|
('ALIGN', (3, 0), (-1, -1), 'CENTER'),
|
|
('ALIGN', (0, 0), (0, -1), 'CENTER'),
|
|
('VALIGN', (0, 0), (-1, -1), 'MIDDLE'),
|
|
]
|
|
tro = TrophyPdf(filename='test01.pdf')
|
|
tro.add_table(table=tb)
|
|
tro.add_table(table=tb, title='A Tittle.........')
|
|
tro.commit()
|