Spaces:
Sleeping
Sleeping
File size: 2,733 Bytes
197a58a 522b639 197a58a 1886799 522b639 197a58a 522b639 197a58a 522b639 197a58a 522b639 197a58a 522b639 197a58a 522b639 197a58a 522b639 |
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 |
from reportlab.lib.pagesizes import A5
from reportlab.lib import colors
from reportlab.platypus import SimpleDocTemplate, Table, TableStyle, Paragraph, Spacer
from reportlab.lib.styles import getSampleStyleSheet
def create_receipt(text, file_name):
# Nettoyage basique
clean = text.replace('*', '').replace('#', '').replace('Reçu', '')
# Extraction des champs simples
lines = [l.strip() for l in clean.split("\n") if l.strip()]
data_map = {}
description_lines = []
for l in lines:
if ":" in l:
key, val = l.split(":", 1)
data_map[key.strip()] = val.strip()
else:
description_lines.append(l)
# Données extraites
client = data_map.get("Client", "")
telephone = data_map.get("Téléphone", "")
adresse = data_map.get("Adresse", "")
date_commande = data_map.get("Date de commande", "")
delai = data_map.get("Délai de livraison estimé", "")
# Description brute (ex: "3 iPhone (1 rouge, 2 blancs)")
description = " ".join(description_lines)
# Styles
styles = getSampleStyleSheet()
title_style = styles["Title"]
normal = styles["Normal"]
# Création du document
doc = SimpleDocTemplate(file_name, pagesize=A5,
leftMargin=25, rightMargin=25, topMargin=25, bottomMargin=25)
elements = []
# Titre
elements.append(Paragraph("FACTURE", title_style))
elements.append(Spacer(1, 12))
# Informations client
info_block = (
f"<b>Client :</b> {client}<br/>"
f"<b>Téléphone :</b> {telephone}<br/>"
f"<b>Adresse :</b> {adresse}<br/>"
f"<b>Date de commande :</b> {date_commande}<br/>"
f"<b>Délai de livraison estimé :</b> {delai}<br/>"
)
elements.append(Paragraph(info_block, normal))
elements.append(Spacer(1, 18))
# Tableau détaillé
table_data = [
["Description", "Quantité"],
[description, description.split()[0] if description.split()[0].isdigit() else ""]
]
table = Table(table_data, colWidths=[200, 60])
table.setStyle(
TableStyle([
("BACKGROUND", (0, 0), (-1, 0), colors.lightgrey),
("TEXTCOLOR", (0, 0), (-1, 0), colors.black),
("ALIGN", (1, 1), (-1, -1), "CENTER"),
("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
("BOX", (0, 0), (-1, -1), 1, colors.black),
("GRID", (0, 0), (-1, -1), 0.5, colors.grey),
("FONTSIZE", (0, 0), (-1, -1), 10),
])
)
elements.append(table)
elements.append(Spacer(1, 24))
# Mention finale
elements.append(Paragraph("Merci pour votre commande.", normal))
# Génération du PDF
doc.build(elements)
|