Uncategorized

how to print (with printer ) a excel sheet with python with print area


I’m trying to launch prints on my printer from python but with a print_area.

I tried this code (code from chat GPT) but it doesn’t work. The printer print the entire sheet and not the print-area i defined.

import win32com.client
import openpyxl


def imprimer_feuille_excel(nom_fichier, nom_feuille):
    # définition de la zone d'impression
    wb = openpyxl.load_workbook(nom_fichier)
    feuille = wb[nom_feuille]
    feuille.print_area="A1:J10"
    wb.save(nom_fichier)

    #impression
    excel = win32com.client.Dispatch("Excel.Application")
    excel.Visible = False  # Pour rendre Excel invisible pendant le processus
    classeur = excel.Workbooks.Open(nom_fichier)
    feuille = classeur.Sheets[nom_feuille]
    feuille.PrintOut()  # Imprimer la feuille
    classeur.Close(SaveChanges=False)  # Fermer le classeur sans sauvegarder les modifications
    excel.Quit()

Even when print_area is defined manually in excel, this code print the entire page (if i could print with print_area defined in excel, it coud be enough for me)

Thanks for the help



Source link

Leave a Reply

Your email address will not be published. Required fields are marked *