Print a PDF File in Python but show the printer properties dialog window before using python

513 views Asked by At

I am currently working on a script that will crop PDF files to my requirements and merge them into a pdf file. This should be printed directly afterwards via the default printer, but using the side printer tray and in A6 format. Therefore I want the script to open the printer properties dialog window before printing.

I am talking about this dialog window

I am grateful for any help, as I have already searched countless hours on the internet

1

There are 1 answers

0
Core taxxe On

I finally found a solution. Hopefully, this also helps you as well/is what you were looking for.

import win32print

name="printername"

# access defaults
PRINTER_DEFAULTS = {"DesiredAccess": win32print.PRINTER_ALL_ACCESS}

# open printer to get the handle
pHandle = win32print.OpenPrinter(name,PRINTER_DEFAULTS)

# get the current properties
properties = win32print.GetPrinter(pHandle, 2)

#get the devmode
pDevModeObj = properties['pDevMode']

#open the printer properties and pass the devmode
win32print.DocumentProperties(0, pHandle, name, pDevModeObj, None, 5)

# reassign the devmode
properties['pDevMode'] = pDevModeObj

# save the printer settings
win32print.SetPrinter(pHandle,2,properties,0)

# close the printer
win32print.ClosePrinter(pHandle)