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
| from reportlab.lib.pagesizes import landscape, A3
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Image
from reportlab.lib.styles import getSampleStyleSheet
import pandas as pd
def create_pdf_with_dataframe(file_path, dataframe, image_path):
# Spécifier la taille de la page en mode paysage et A3 (plus grande)
doc = SimpleDocTemplate(file_path, pagesize=landscape(A3), topMargin=30)
styles = getSampleStyleSheet()
style_heading = styles["Heading1"]
# Créer un tableau pour le DataFrame
table_data = [dataframe.columns.values.tolist()] + dataframe.values.tolist()
table = Table(table_data)
# Appliquer des styles au tableau
table.setStyle(TableStyle([('ALIGN', (0, 0), (-1, -1), 'CENTER'),
('FONT', (0, 0), (-1, 0), 'Helvetica-Bold'),
('BACKGROUND', (0, 0), (-1, 0), 'grey'),
('TEXTCOLOR', (0, 0), (-1, 0), 'white'),
('GRID', (0, 0), (-1, -1), 1, 'black')]))
# Ajouter le titre
title = Paragraph("ROP", style_heading)
# Construction des éléments
elements = []
# Ajouter l'image en haut à gauche
image = Image(image_path, width=120, height=85)
image.wrapOn(doc, doc.width, doc.topMargin) # Pour ajuster la taille de l'image selon la marge supérieure
image.drawOn(doc, 30, doc.height - 120) # Positionner l'image aux coordonnées (30, hauteur du document - 120)
# Ajouter le titre
elements.append(title)
# Ajouter le tableau
elements.append(table)
# Générer le PDF
doc.build(elements)
# Exemple d'utilisation :
site_id = "XXXX"
account_code = "XXXX"
df = data(site_id, account_code) # Assurez-vous que cette fonction retourne un DataFrame
image_path = "c:\\Users\\xxxx\\Desktop\\Factures detail\\img\\xxx.png" # Remplacez par le chemin de votre image
create_pdf_with_dataframe("c:\\Users\\xxx\\Desktop\\Factures detail\\example.pdf", df, image_path) |
Partager