• # Il manque des éléments

    Posté par (site web personnel) . En réponse au message Automatisation PDF avec ID unique + logs dans fichier xls. Évalué à 2.

    Bonjour, j'ignore le but de l'opération mais je serais curieux de savoir à quoi tout ça peut servir. Le code incomplet ne permet pas d'avoir une idée exacte de l’approche utilisée et donc des potentiels soucis. Étant curieux, j'ai complété le script avec quelques fonction minimales

     import sys
     from PIL import Image, ImageDraw, ImageFont
    -from reportlab.lib.pagesizes import letter
    +from reportlab.lib.pagesizes import letter, A4
     from reportlab.pdfgen import canvas
     import uuid
     import xlwings as xw
    @@ -11,6 +11,28 @@ import tempfile
     # Les fonctions generate_unique_id, add_id_to_image, et create_pdf_with_image restent inchangées
    +def generate_unique_id():
    + return str(uuid.uuid4())
    +
    +def add_id_to_image(image_path, output_image_path, unique_id, count):
    + img = Image.open(image_path)
    + draw = ImageDraw.Draw(img)
    + # font = ImageFont.truetype(<font-file>, <font-size>)
    + font = ImageFont.truetype("DejaVuSans.ttf", 16)
    + # draw.text((x, y),"Sample Text",(r,g,b))
    + draw.text((0, 0), unique_id,(255,255,255),font=font)
    + img.save(output_image_path)
    + print("add id to image...")
    +
    +def create_pdf_with_image(output_image_path, pdf_path):
    + c = canvas.Canvas(pdf_path, pagesize=letter)
    + c.drawString(50, 50, "Hello world!")
    + w, h = A4
    + c.ellipse(50, h - 50, w + 150, h - 50)
    + c.drawImage(output_image_path, 50, h - 200)
    + c.showPage()
    + c.save()
    +
     def add_to_excel(excel_path, email, unique_id, pdf_path, creation_date, creation_time):
     print(f"Tentative d'ajout au fichier Excel : {excel_path}")

    J'ai créé un fichier basique invitations_info.xlsm dans LibreOffice et utilisé une bête image jpg appelée invitation.jpg. Mais étant sous Linux, le programme crash car xlwings ne fonctionne correctement que sous Windows. Cette bibliothèque sert à piloter Microsoft Excell et elle ne semble fonctionner que sous Windows ou macOS. ne disposant pas de ces systèmes, je ne peux pas aller plus loin.

    Pour info, voilà le résultat quand je lance le script:

    (venv) [arnaud aldebaran test]: python prout.py 
    Entrez l'adresse e-mail du destinataire: plop@example.com
    Entrez le nombre d'invitations à générer: 3
    Chemin du script : /tmp/test
    Chemin du fichier Excel : /tmp/test/invitations_info.xlsm
    add id to image...
    PDF généré: invitations_generees/invitation_35664ee3-77ef-4e3c-8b82-86f98528032f_1.pdf
    Tentative d'ajout au fichier Excel : /tmp/test/invitations_info.xlsm
    Chemin du fichier Excel : /tmp/test/invitations_info.xlsm
    Ouverture du fichier Excel existant : /tmp/test/invitations_info.xlsm
    Erreur lors de l'écriture dans le fichier Excel: The interactive mode of xlwings is only supported on Windows and macOS. On Linux, you can use xlwings Server or xlwings Reader.
    Traceback (most recent call last):
     File "/tmp/test/prout.py", line 46, in add_to_excel
     wb = xw.Book(excel_path)
     ^^^^^^^^^^^^^^^^^^^
     File "/tmp/venv/lib/python3.12/site-packages/xlwings/main.py", line 914, in __init__
     for app in apps:
     File "/tmp/venv/lib/python3.12/site-packages/xlwings/main.py", line 258, in __iter__
     for app in self.impl:
     ^^^^^^^^^
     File "/tmp/venv/lib/python3.12/site-packages/xlwings/main.py", line 5262, in impl
     raise XlwingsError(
    xlwings.XlwingsError: The interactive mode of xlwings is only supported on Windows and macOS. On Linux, you can use xlwings Server or xlwings Reader.
    During handling of the above exception, another exception occurred:
    Traceback (most recent call last):
     File "/tmp/test/prout.py", line 147, in <module>
     main()
     File "/tmp/test/prout.py", line 112, in main
     temp_file = add_to_excel(excel_path, email, unique_id, pdf_path, creation_date, creation_time)
     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     File "/tmp/test/prout.py", line 61, in add_to_excel
     wb = xw.Book(excel_path)
     ^^^^^^^^^^^^^^^^^^^
     File "/tmp/venv/lib/python3.12/site-packages/xlwings/main.py", line 914, in __init__
     for app in apps:
     File "/tmp/venv/lib/python3.12/site-packages/xlwings/main.py", line 258, in __iter__
     for app in self.impl:
     ^^^^^^^^^
     File "/tmp/venv/lib/python3.12/site-packages/xlwings/main.py", line 5262, in impl
     raise XlwingsError(
    xlwings.XlwingsError: The interactive mode of xlwings is only supported on Windows and macOS. On Linux, you can use xlwings Server or xlwings Reader.