0

I am developing a Python application where it is possible to print an image. I was able to modify some document properties through the win32print library with this code:

import win32print

PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS} 
printer_name = 'DYMO LabelWriter 450'
handle = win32print.OpenPrinter(printer_name, PRINTER_DEFAULTS) 

# Get the default properties for the printer
properties = win32print.GetPrinter(handle, 2)
devmode = properties['pDevMode']

devmode.PaperWidth = 600
devmode.PaperLength = 600
devmode.PaperSize = 0
properties["pDevMode"]=devmode 

win32print.SetPrinter(handle,2,properties,0)

but I don't know how to change advanced properties such as, for example, the document format (A3, A4, ..)

  • it seems that this post has a *ton* of information on the subject, does that help at all? https://stackoverflow.com/questions/64867896/setting-default-printer-custom-page-size-with-python – Random Davis Jan 27 '22 at 16:40
  • yes, unfortunately there are only solutions that do not fit the advanced properties. Only the answer to this question: https://stackoverflow.com/questions/5555453/python-win32print-changing-advanced-printer-options oncerns the problem but does not solve it. – Sante Altamura Jan 27 '22 at 16:48

1 Answers1

0

Try this:

def AddCustomPaperSize(str_width_mm, str_height_mm, printer_name='PDF24', level=2):
    width_mm = int(str_width_mm if isinstance(str_width_mm, str) and str_width_mm.isdigit() else 0)
    height_mm = int(str_height_mm if isinstance(str_height_mm, str) and str_height_mm.isdigit() else 0)
    width_mm = int(height_mm if bool(width_mm > height_mm) else width_mm)
    height_mm = int(width_mm if bool(width_mm > height_mm) else height_mm)
    if bool(width_mm > 0 < height_mm):
        # Get a handle for the default printer
        device_name = win32print.GetDefaultPrinter()
        print("Default printer name: " + device_name)
        format_name = 'CustomSize ' + str(width_mm) + 'x' + str(height_mm)
        if printer_name != device_name: win32print.SetDefaultPrinter(printer_name)
        PRINTER_DEFAULTS = {'DesiredAccess': win32print.PRINTER_ALL_ACCESS}
        width, height = int(float(width_mm * 1000)), int(float(height_mm * 1000))
        custom_form = ({'Flags': 0, 'Name': format_name, 'Size': {'cx': width, 'cy': height},
                        'ImageableArea': {'left': 0, 'top': 0, 'right': width, 'bottom': height}})

        hprinter = win32print.OpenPrinter(device_name, PRINTER_DEFAULTS)
        if bool(hprinter):
            try:
                win32print.GetForm(hprinter, format_name)
                win32print.DeleteForm(hprinter, format_name)
            except: pass
            try:
                win32print.AddForm(hprinter, custom_form)
                attributes = win32print.GetPrinter(hprinter, level)
                win32print.SetPrinter(hprinter, level, attributes, 0)
                print("Custom paper size: {}x{}".format(width_mm, height_mm))
                print("... Script Add custom paper size successfully")
                win32print.ClosePrinter(hprinter)
            except Exception as error:
                print(error)


width_mm, height_mm, printer_name = sys.argv[1], sys.argv[2], sys.argv[3]
print("Start set custom printer format: " + printer_name)
AddCustomPaperSize(width_mm, height_mm, printer_name)
Sunderam Dubey
  • 1
  • 11
  • 20
  • 40