INIT rapport.py
This commit is contained in:
parent
fd3b4e3717
commit
d554ac16ea
105
pycen/rapport.py
Normal file
105
pycen/rapport.py
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
#!/usr/bin/env python
|
||||||
|
# -*- coding: UTF-8 -*-
|
||||||
|
|
||||||
|
import pandas
|
||||||
|
import io
|
||||||
|
from matplotlib.figure import Figure
|
||||||
|
from reportlab.lib.enums import TA_JUSTIFY
|
||||||
|
from reportlab.lib.pagesizes import A4
|
||||||
|
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, PageBreak
|
||||||
|
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
||||||
|
from reportlab.pdfgen import canvas
|
||||||
|
from reportlab.lib.units import mm, inch
|
||||||
|
|
||||||
|
|
||||||
|
# Ajout d'un style justifié
|
||||||
|
styles = getSampleStyleSheet()
|
||||||
|
styles.add(ParagraphStyle(name='Justify', alignment=TA_JUSTIFY))
|
||||||
|
|
||||||
|
# page size
|
||||||
|
page_size = A4
|
||||||
|
page_with, page_height = page_size
|
||||||
|
|
||||||
|
# Margins
|
||||||
|
right_margin = 72
|
||||||
|
left_margin = 72
|
||||||
|
top_margin = 40
|
||||||
|
bottom_margin = 18
|
||||||
|
|
||||||
|
page_editable_width = page_with - (left_margin + right_margin)
|
||||||
|
page_editable_height = page_height - (top_margin + bottom_margin)
|
||||||
|
|
||||||
|
|
||||||
|
class Rapport(object):
|
||||||
|
def __init__(self, nom='rapport.pdf', titre="", pied_de_page=""):
|
||||||
|
self.doc = SimpleDocTemplate(nom,
|
||||||
|
pagesize=page_size,
|
||||||
|
rightMargin=right_margin,
|
||||||
|
leftMargin=left_margin,
|
||||||
|
topMargin=top_margin,
|
||||||
|
bottomMargin=bottom_margin)
|
||||||
|
|
||||||
|
self.story = list()
|
||||||
|
self.titre = titre
|
||||||
|
self.pied_de_page = pied_de_page
|
||||||
|
|
||||||
|
def image(self, img):
|
||||||
|
if isinstance(img, Figure):
|
||||||
|
img = self.__fig_to_img(img)
|
||||||
|
im = Image(img)
|
||||||
|
im._restrictSize(page_editable_width, page_editable_height)
|
||||||
|
self.story.append(im)
|
||||||
|
self.spacer()
|
||||||
|
|
||||||
|
def paragraph(self, text, fontsize=12, style="Normal"):
|
||||||
|
ptext = u'<font size={0:d}>{1:s}</font>'.format(fontsize, text)
|
||||||
|
self.story.append(Paragraph(ptext, styles[style]))
|
||||||
|
self.spacer()
|
||||||
|
|
||||||
|
def table(self, data):
|
||||||
|
TableStyle()
|
||||||
|
if isinstance(data, pandas.Series):
|
||||||
|
data = data.iteritems()
|
||||||
|
self.story.append(Table(data))
|
||||||
|
|
||||||
|
def df(self, df):
|
||||||
|
from reportlab.lib import colors
|
||||||
|
|
||||||
|
df = df.reset_index()
|
||||||
|
lista = [df.columns[:, ].values.astype(str).tolist()] + df.values.tolist()
|
||||||
|
ts = [('LINEABOVE', (0, 0), (-1, 0), 1, colors.purple),
|
||||||
|
('LINEBELOW', (0, 0), (-1, 0), 1, colors.purple),
|
||||||
|
('FONT', (0, 0), (-1, 0), 'Times-Bold'),
|
||||||
|
]
|
||||||
|
self.story.append(Table(lista, style=ts))
|
||||||
|
|
||||||
|
def spacer(self, width=1, height=12):
|
||||||
|
self.story.append(Spacer(width, height))
|
||||||
|
|
||||||
|
def page(self):
|
||||||
|
self.story.append(PageBreak())
|
||||||
|
|
||||||
|
def __fig_to_img(self, fig):
|
||||||
|
imgdata = io.BytesIO()
|
||||||
|
fig.savefig(imgdata, format='jpg', dpi=300, bbox_inches='tight')
|
||||||
|
imgdata.seek(0)
|
||||||
|
return imgdata
|
||||||
|
|
||||||
|
def myFirstPage(self, canvas, doc):
|
||||||
|
canvas.saveState()
|
||||||
|
canvas.setFont('Times-Bold', 22)
|
||||||
|
canvas.drawCentredString(page_with / 2.0, page_height - 108, self.titre)
|
||||||
|
canvas.setFont('Times-Roman', 9)
|
||||||
|
canvas.drawString(0.5 * inch, 0.5 * inch, "%s" % self.pied_de_page)
|
||||||
|
canvas.restoreState()
|
||||||
|
|
||||||
|
def myLaterPages(self, canvas, doc):
|
||||||
|
canvas.saveState()
|
||||||
|
canvas.setFont('Times-Roman', 9)
|
||||||
|
canvas.drawString(0.5 * inch, 0.5 * inch, "%s" % self.pied_de_page)
|
||||||
|
canvas.drawString(4 * inch, 0.5 * inch, "Page %d " % doc.page)
|
||||||
|
canvas.restoreState()
|
||||||
|
|
||||||
|
def save(self):
|
||||||
|
self.doc.build(self.story, onFirstPage=self.myFirstPage, onLaterPages=self.myLaterPages)
|
||||||
|
|
||||||
Loading…
x
Reference in New Issue
Block a user