问题
I'm new to python and want to change the printer preference settings. I'm using win32print library and need to change PyDEVMODE Object properties given on link http://timgolden.me.uk/pywin32-docs/PyDEVMODE.html as per requirement.But facing problem in crating object.
I have tried this- Python win32print changing advanced printer options
getting error "name 'pDevModeObj' is not defined"
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter('300LN1', PRINTER_DEFAULTS)
properties = win32print.GetPrinter(pHandle, 2)
pDevModeObj.Orientation = 2
properties["pDevMode"]=pDevModeObj
win32print.SetPrinter(pHandle,2,properties,0)
回答1:
This code will obviously give error.
As you are using pDevModeObj
before its creation.
Even in next line, you are not assigning value to pDevModeObj
but rather assigning variable pDevModeObj
to properties["pDevMode"]
, which does not work in Python.
The fix to your existing solution would be:
PRINTER_DEFAULTS = {"DesiredAccess":win32print.PRINTER_ALL_ACCESS}
pHandle = win32print.OpenPrinter('300LN1', PRINTER_DEFAULTS)
properties = win32print.GetPrinter(pHandle, 2)
pDevModeObj = properties["pDevMode"]
pDevModeObj.Orientation = 2
win32print.SetPrinter(pHandle,2,properties,0)
win32print.ClosePrinter(pHandle)
来源:https://stackoverflow.com/questions/58549573/how-to-change-printer-preference-settings-using-python